function inside function... need basic help
Moderator: General Moderators
function inside function... need basic help
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?
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?
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);
}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 20yep, those actualy help...
what about this
can i do that?
BTW: nice awards in your signature
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);BTW: nice awards in your signature
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);- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: function inside function... need basic help
Yespedrotuga wrote:can i call a function inside another one?
Functions have only access to the variables inside their scope.pedrotuga wrote:if i call a function inside another funcion the parameters have to be picked from the inside of the parent function right?
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.
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...
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...
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.astions wrote:$b would output "hi there!" in this case.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);
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.
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);
}
}
}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 )Jcart wrote:I still don't see why it's neccesary to have functions within functions
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
Closures are very useful sometimesJcart wrote: I honestly can't think of a situation where a function inside a function is the best course of action.