Patrick3002 Posted March 14, 2007 Share Posted March 14, 2007 I was wondering if it is possible to to have a function inside of a custom created function? Heres an example: <?php function orderBy() { switch ($selorder) { case snasc: $field = "sname"; $ad = "ASC"; $ordered = "Service Name (ASC)"; break; case sndesc: $field = "sname"; $ad = "DESC"; $ordered = "Service Name (DESC)"; break; default: $field = "sname"; $ad = "ASC"; $ordered = "Service Name (ASC)"; } } ?> Link to comment https://forums.phpfreaks.com/topic/42760-solved-function-inside-of-custom-function/ Share on other sites More sharing options...
per1os Posted March 14, 2007 Share Posted March 14, 2007 A switch case is not really a "function" it can be inside a function as it acts just like an if/elseif/else statement. The problem here lies within the case portion, try this: switch ($selorder) { case 'snasc': $field = "sname"; $ad = "ASC"; $ordered = "Service Name (ASC)"; break; case 'sndesc': $field = "sname"; $ad = "DESC"; $ordered = "Service Name (DESC)"; break; default: $field = "sname"; $ad = "ASC"; $ordered = "Service Name (ASC)"; } ?> Those words need to be surrounded by quotation marks or else php takes it as a constant. Link to comment https://forums.phpfreaks.com/topic/42760-solved-function-inside-of-custom-function/#findComment-207510 Share on other sites More sharing options...
Patrick3002 Posted March 14, 2007 Author Share Posted March 14, 2007 The case works fine outside of the function though... I'll try Link to comment https://forums.phpfreaks.com/topic/42760-solved-function-inside-of-custom-function/#findComment-207515 Share on other sites More sharing options...
Patrick3002 Posted March 14, 2007 Author Share Posted March 14, 2007 I execute the function with orderBy(); right? Link to comment https://forums.phpfreaks.com/topic/42760-solved-function-inside-of-custom-function/#findComment-207520 Share on other sites More sharing options...
per1os Posted March 14, 2007 Share Posted March 14, 2007 Ding, my bad man. If field, ad and ordered are not defined as global they are local only to the function. You can define them as being global or return them in an array. You also have to pass the parameter in or set that global also. IE: <?php function orderBy($selorder) { global $field, $ad, $ordered; switch ($selorder) { case snasc: $field = "sname"; $ad = "ASC"; $ordered = "Service Name (ASC)"; break; case sndesc: $field = "sname"; $ad = "DESC"; $ordered = "Service Name (DESC)"; break; default: $field = "sname"; $ad = "ASC"; $ordered = "Service Name (ASC)"; } } ?> OR <?php function orderBy($selorder) { switch ($selorder) { case snasc: $field = "sname"; $ad = "ASC"; $ordered = "Service Name (ASC)"; break; case sndesc: $field = "sname"; $ad = "DESC"; $ordered = "Service Name (DESC)"; break; default: $field = "sname"; $ad = "ASC"; $ordered = "Service Name (ASC)"; } return array("field" => $field, "ad" => $ad, "ordered" => $ordered); } $getOrder = orderBy("sndesc"); foreach ($getOrder as $key => $val) { // set the key equal to a variable with the value; $$key = $val; } ?> Hope that helps. Link to comment https://forums.phpfreaks.com/topic/42760-solved-function-inside-of-custom-function/#findComment-207526 Share on other sites More sharing options...
Patrick3002 Posted March 14, 2007 Author Share Posted March 14, 2007 Awesome, that works perfect thanks alot frost!! , Patrick Link to comment https://forums.phpfreaks.com/topic/42760-solved-function-inside-of-custom-function/#findComment-207530 Share on other sites More sharing options...
coplan Posted March 14, 2007 Share Posted March 14, 2007 One minor comment... You need to be very conscious of local vs. global scope. Anything defined outside the function will be ignored unless you use the global command or the $GLOBALS[] variable. On the same accord, anything defined within the function will be unavailable to the global scope unless it is returned. Example: $some_var = 'This is global scope.'; $final_var = some_function(); // Option 1 $another_final_var = another_function($some_var); // Option 2 - which is what I would recommend function some_function() { global $some_var; // this pulls $some_var into local scope to be acted upon. $new_var = $some_var.' But I can act upon it with the global command.'; return $new_var; // the combined will then be returned. NOTE that $new_var never leaves local scope. } function another_function($some_var) { // $some_var is now part of local scope $new_var = $some_var.' This is another option.'; return $new_var; // again, $new_var never leaves local scope. } Link to comment https://forums.phpfreaks.com/topic/42760-solved-function-inside-of-custom-function/#findComment-207538 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.