Page 1 of 1

function inside function... need basic help

Posted: Mon Jun 19, 2006 1:53 pm
by pedrotuga
how does this work?
can i call a function inside another one?
what should i be aware of?

if i call a function inside another funcion the parameters have to be picked from the inside of the parent function right?

Posted: Mon Jun 19, 2006 1:57 pm
by Benjamin

Code: Select all

function One() {
  $a = 'I set A';
  $b = 'I Set B';

  function Two($b) {
    echo $a; // outputs nothing
    echo $b; // outputs 'I Set B';
  }

  Two($b);
}
? That help?

Posted: Mon Jun 19, 2006 2:00 pm
by Luke
Do you mean just this?

Code: Select all

function_one($a){
    $new_val = $a + 10;
    echo $new_val;
}
function_two($b){
    function_one($b);
}
function_two(5); // Will output 20

Posted: Mon Jun 19, 2006 2:03 pm
by pedrotuga
yep, those actualy help...

what about this

Code: Select all

function One() {
  $a = 'I set A';
  $b = 'I Set B';

  function Two($b) {
    echo $a; // outputs nothing
    echo $b; // outputs 'I Set B';
  }


} 

$a="hi there!";
Two($a);
can i do that?

BTW: nice awards in your signature ;)

Posted: Mon Jun 19, 2006 2:10 pm
by Benjamin

Code: Select all

function One() {
  $a = 'I set A';
  $b = 'I Set B';

  function Two($b) {
    echo $a; // outputs nothing
    echo $b; // outputs 'I Set B';
  }


} 

$a="hi there!";
Two($a);
$b would output "hi there!" in this case.

Re: function inside function... need basic help

Posted: Mon Jun 19, 2006 2:14 pm
by John Cartwright
pedrotuga wrote:can i call a function inside another one?
Yes
pedrotuga wrote:if i call a function inside another funcion the parameters have to be picked from the inside of the parent function right?
Functions have only access to the variables inside their scope.

I honestly can't think of a situation where a function inside a function is the best course of action. Perhaps if you share what you want to do we can assist furthur.

Posted: Mon Jun 19, 2006 2:28 pm
by pedrotuga
Its necessary...
i am coding this webform where a user can pick from a list of curses and then insert his/her grade.

the thing is that there are 40 compulsory courses and up to 6 optional... the six optional are picked from a list of about 100.

i made a function to show the wole form... but optional dropdown boxes... they are six of them

this way i can call the show_form() function that uses a cicle to print all the 40 courses and another to call a a fiction that shows the dropdownbox 6 times.

ok.. a bit macarronic english... hope is understoodable...

Posted: Mon Jun 19, 2006 2:38 pm
by John Cartwright
I still don't see why it's neccesary to have functions within functions :?

Posted: Mon Jun 19, 2006 2:43 pm
by feyd
astions wrote:

Code: Select all

function One() {
  $a = 'I set A';
  $b = 'I Set B';

  function Two($b) {
    echo $a; // outputs nothing
    echo $b; // outputs 'I Set B';
  }


} 

$a="hi there!";
Two($a);
$b would output "hi there!" in this case.
Slight factual error: function Two() won't exist until after function One() has been run once. Each call afterward to one() would then create a function redefinition error too.

Posted: Mon Jun 19, 2006 2:45 pm
by Benjamin
Well LOL. I wrote this years ago. I would probably write it a lot different now, but it's a function that calls itself, has if and while loops, and global variables. This function is used to recursively open and close HTML tags for webpages built on the fly from a database.

I use this function in a webbased website builder.

Code: Select all

function open_record($record_number) {
  global $page_number, $connect, $closed_tags, $opened_tags, $mode;

  if ($opened_tags[$record_number] == false) {
    render_object_open($record_number);
    $opened_tags[$record_number] = true;
  }

  if (has_children($record_number) == true) {
    $object_id = "select object_id from webpage_objects where record_number='" . $record_number . "'";
    $execute = mysql_query($object_id,$connect);
    $data = mysql_fetch_assoc($execute);
    $object_id = $data['object_id'];
    $open_children = true;
    $get_kids = "select record_number from webpage_objects where parent_object_id='" . $object_id . "' order by object_id asc";
    $execute = mysql_query($get_kids,$connect);
    while (($open_children == true) and ($data = mysql_fetch_assoc($execute))) {
      $record_to_check = $data['record_number'];
      if ($opened_tags[$record_to_check] != true) {
        $open_children = false;
        $mode = 'open';
        open_record($record_to_check);
      } else {
        $mode = 'close';
      }
    }
  } else {
    $mode = 'close';
  }

  if ($mode == 'close') {
    if ($closed_tags[$record_number] != true) {
    render_object_close($record_number);
    $closed_tags[$record_number] = true;
    }

    $get_parent = "select parent_object_id from webpage_objects where record_number='" . $record_number . "'";
    $execute = mysql_query($get_parent,$connect);
    $data = mysql_fetch_assoc($execute);
    if ($data['parent_object_id'] != 0) {
      $get_record_number = "select record_number from webpage_objects where object_id='" . $data['parent_object_id'] . "' and page_number='" . $page_number . "'";
      $execute = mysql_query($get_record_number,$connect);
      $data = mysql_fetch_assoc($execute);
      $record_number = $data['record_number'];
      open_record($record_number);
    }
  }
}

Posted: Mon Jun 19, 2006 2:48 pm
by pedrotuga
Jcart wrote:I still don't see why it's neccesary to have functions within functions :?
in fact i could use a cicle... but i dont get the poit of using a cicle to run like 3 iterations ( ok they are 6 )

in this case i actually have

print_dd(1);
print_dd(5);
print_dd(66);
print_dd(2);
print_dd(674);
print_dd(332);

the parameters coub be inserted by the use for instance... i dunno... maybe its jusst me... but if the wole form is outputed by a function ( cant see a reason not to do that ) should i stay away from functions after that?

Re: function inside function... need basic help

Posted: Mon Jun 19, 2006 3:02 pm
by Weirdan
Jcart wrote: I honestly can't think of a situation where a function inside a function is the best course of action.
Closures are very useful sometimes :).