Reputation: 63
I have a main function, inside it there is another function which heavily relates on several functions. I need to terminate the second function whenever some criteria meet. This, by itself, comes from the result of those, let's say, inner functions. So, how can I terminate only the second function that doesn't affect the main function being running. I'm aware of die
and exit
. however, those two, terminate the whole process.
function main($string) {
//some code here...
function second($content) {
//more code...
inner_one($evaluate);
//need to stop second function if some criteria met here, but the rest of the "main function" should run normally
}
//more codes...
}
function inner_one($evaluate) {
//some evaluation here
//the result of this function should make "second function" stop or allow running
}
If I use die
or exit
inside inner function the whole process fail. How to get that to work?
Upvotes: 2
Views: 2072
Reputation: 14730
Recall that when you write:
function foo(){
// code....
}
You define function foo
. This function will not be executed unless called, which may be written in different fashions.
Either directly:
foo();
Or by using existing functions of PHP (call_user_func
), or by assigning the name of the function to a variable and using that variable as the function:
$fun = 'foo';
$fun(); // calls foo
Function definitions may occur anywhere in PHP code where a statement is valid. That means that second
as written in your code snippet is a function definition, which will be visible from any other place in the program. The subtle point is that this definition will only be enabled when main
is being executed(*), as opposed to having that function definition at global scope (outside of any other function or class).
Although this a quite powerful feature, unless you want to make sure that main
is being executed before making second
available, or if you want to pick one definition at runtime among several definitions of second
, I suggest to avoid putting function definitions inside other function definitions.
Now, when you call second
, your function will behave as any other regular PHP function, so simply use return
to signify that you want the execution flow to leave the function (ie. that the PHP interpreter exits the function and keeps running to the next instruction after the call to second
).
exit
and die
are indeed instructions interrupting the PHP script execution, and may be used anywhere an instruction is valid.
A simple way to implement your requirements is to have inner_one
return a value which may be interpreted as a boolean — the simplest is to use a boolean directly of course:
function inner_one($evaluate){
// some computation yielding $some_state
if ($some_state == SOME_GOOD_VALUE)
return true;
else return false;
}
Or more effectively :
function inner_one($evaluate){
// some computation
return ($some_state == SOME_GOOD_VALUE);
}
second
may then act accordingly to that returned value, by following a similar algorithm:
function second(){
// some computation
if (inner_one($evaluate)){
// carry on computation
}
}
Or, to avoid too many code nestings:
function second(){
// some computation
if (!inner_one($evaluate))
return ;
// carry on computation
}
(*) to be precise, any definition is enabled as soon as the execution flow has evaluated it, which explains why you may write functions inside others and have this neat mechanism
Upvotes: 0
Reputation: 3367
For this, you should probably take out your second
function to make it an external one. Then use return
on your inner functions. die
and exit
are fairly uncommonly needed. Typically only for error handling and such when your entire application does need to come to a complete halt.
For this specific code though, you should add a return value that you check if you need to exit.
Upvotes: 0
Reputation: 7134
Hope you will get idea from here.
function main($string) {
//some code here...
function second($content) {
//more codes
$status=inner_one($evaluate);
if(!$status)//if your inner_one does not run successfully return false
{
return false;
}
//write your other codes for second
//return false if it gets any error
//more codes
return true;
}
//more codes...
}
function inner_one($evaluate) {
//your codes going here
//if you find some error return false
/// ............
return true;//at the end
}
Upvotes: 1