I still have troubles with this here
function first() {
function second() {
// ..
}
}
if ($_GET['action'] == 'second') first(second());
| SitePoint Sponsor |
I still have troubles with this here
function first() {
function second() {
// ..
}
}
if ($_GET['action'] == 'second') first(second());
If you put a function within a function, you can only fire said function from inside the parent function.
So you have two choices:
orPHP Code:<?php
function first($val){
///
}
function second(){
///
return 'something';
}
first(second());
?>
PHP Code:<?php
function first($command){
switch($command){
case 'second':
//execute code here
break;
}
}
first('second');
?>
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
first();second();
It depends what you want to accomplish really.
P.s. thanks for the siglink![]()
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona





No, if you define a function inside a function, you can call the inner function, but you must call the outer function to define the inner function. But if you call the outer function twice, then the inner function will be defined twice, and PHP will throw a fatal error.
Function declarations are still very procedural; however, when in the global/top scope, you can put function declarations in any order, even before you call them.
I guess what Im trying to ask is this:
I have a function inside a function, I want to call it from this link:
<a href='?action=first&second&page=".$page[$ids]."'


Could be you don't really want nested functions. Do you just want to call function2 from within function1?
If you really do want it that way, then what you posted above is true.
first();second();
But, as sk89q said, you can't call first() again in that script. (unless you modified it to check if second() was defined before defining it)
- Robert
I decided to drop my doubled up function,
and go with ?action=function1&do=function2 -- where do=function2 is just a get request inside of function 1





That's a better way to do it.
However, if you really wanted to do it your original way (not recommended)...
http://example.com/test.php?action=member,burnPHP Code:<?php
$action = explode(",", $_GET['action'], 2);
// Outer function
function member() {
global $member;
$member = $_GET['id'];
// Inner function
function burn() {
global $member; // burn() has NO relation to member(), so we need to pollute the global namespace with $member
echo 'Burning member $member...';
}
// Inner function
function create() {
global $member; // create() has NO relation to member(), so we need to pollute the global namespace with $member
echo 'Creating member $member...';
}
}
if (in_array($action[0], array('member', 'record', 'baby') /* Allowed outer functions */)) {
if (!function_exists($action[1])) { // The inner function should not be defined yet
$action[0](); // This will define the inner function
if (function_exists($action[1])) { // Now it should exist
$action[1](); // Call inner function
} else {
echo "Invalid action (3)";
exit;
}
} else {
echo "Invalid action (2)";
exit;
}
} else {
echo "Invalid action (1)";
exit;
}
Bookmarks