php - Call a function within a function display both within array -


how call function within function?

function tt($data,$s,$t){     $data=$data;     echo '[[';     print_r($data);     echo ']]';     if($t==0){         $data[]=$s;         tt($data,'two',1);     }else{         $data[]=$s;     }     return $data; } print_r(tt('','one',0)); 

i want 'two' shown within array like

$o[]='one'; $o[]='two'; print_r($o); 

function tt($s, $t, array $data = array()) {     $data[] = $s;     if ($t == 0) {         $data = tt('two', 1, $data);     }     return $data; }  print_r(tt('one', 0)); 

this that's needed.

  1. put array last argument , make optional, because don't need on initial call.
  2. when calling tt recursively, need "catch" return data, otherwise recursive call nothing of lasting value.
  3. no need else, since you're going append entry array no matter , don't need write twice.

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -