function inside function... need basic help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

function inside function... need basic help

Post 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?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

Post 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 ;)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: function inside function... need basic help

Post 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.
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

Post 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...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I still don't see why it's neccesary to have functions within functions :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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);
    }
  }
}
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

Post 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?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: function inside function... need basic help

Post 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 :).
Post Reply