Aug 28th, 2011, 02:20 PM #1
Thread Starter
Fanatic Member
[RESOLVED] exit a php function in middle and continue other lines of codehi,
i have this codeCode:
but i want it to print
HELLO WORLD
It does not print world due to the exit function in the function... Please help me to get it work even if exit is executed... :-[
Aug 28th, 2011, 04:13 PM #2
Re: exit a php function in middle and continue other lines of code
I'm guessing what you're looking for is 'return' and not 'exit'.
'return' is a language construct that allows you to exit a function [with possible return value].
'exit' is a function that stops execution for the entire script.Delete it. They just clutter threads anyway.
Aug 28th, 2011, 09:47 PM #3
Thread Starter
Fanatic Member
Re: exit a php function in middle and continue other lines of code
Originally Posted by TheBigBI'm guessing what you're looking for is 'return' and not 'exit'.
'return' is a language construct that allows you to exit a function [with possible return value].
i also tried return, But php keep doing something after return, it executes the whole code and then return. and that cause script to failure Maximum Execution Time 30 Seconds [ Error Comes ]
'exit' is a function that stops execution for the entire script.Aug 29th, 2011, 12:03 AM #4
Re: exit a php function in middle and continue other lines of code
This has no meaning.
PHP Code:
This also has no meaning. It is the same as:function abc[]
{
return;
}
PHP Code:
This:function abc[] {}
PHP Code:
function abc[] {}
echo
will output:'HELLO';
abc[];
echo 'WORLD';
But php keep doing something after return, it executes the whole code and then return. and that cause script to failure Maximum Execution Time 30 Seconds [ Error Comes ]
This means you have created an infinite loop.Post the code that you tried.
Aug 29th, 2011, 05:51 AM #5
Re: exit a php function in middle and continue other lines of code
Am I write assuming you want something like this:
PHP Code:
OUTPUT:
&HELLOAug 29th, 2011, 11:02 AM #6
Thread Starter
Fanatic Member
Re: exit a php function in middle and continue other lines of code
i have noticed, that return still continues to the end of the loop, i just want to come out of the function and then continue rest script
here is the code
Code:
i have just found a temp solution for now, to redirect to another page instead of exit;Aug 29th, 2011, 07:37 PM #7
Re: exit a php function in middle and continue other lines of code
Ah... you didn't mention recursion.
Returning does exit the function. The problem is that you're expecting it to exit the whole recursion loop, but instead what it actually does is exit the current stack frame only.
One technique you could use is to return a value [true or false] to indicate whether or not to continue recursing:
PHP Code:
Traversing a tree can be done without recursion, but it is not as intuitive.Also, you avoid using echo to emit HTML.
Aug 29th, 2011, 07:41 PM #8
Re: exit a php function in middle and continue other lines of code
Also also, it strikes me that you might be able to avoid recursion altogether [and many queries] if you restructured your database.
Aug 29th, 2011, 09:04 PM #9
Thread Starter
Fanatic Member
Re: exit a php function in middle and continue other lines of code
yes,
it works now :-]
Thank you penagateSep 5th, 2011, 07:18 AM #10
Thread Starter
Fanatic Member
Re: [RESOLVED] exit a php function in middle and continue other lines of code
Hi,
i don't know why i can't get out of the loop from the specific place only.is it possible to exit from the recursive function below and process other task in the script ? without using of exit[0] function?
Please make it out after printing the red line in the code.
Code:
Sep 5th, 2011, 08:16 AM #11
Re: [RESOLVED] exit a php function in middle and continue other lines of code
You need to check the return value like I did in post 7.
Sep 8th, 2011, 03:38 PM #12
Thread Starter
Fanatic Member
Re: [RESOLVED] exit a php function in middle and continue other lines of code
yes got it working. :-] thank you
Php exit function and continue
The exit[] function in PHP is an inbuilt function which is used to output a message and terminate the current script. The exit[] function only terminates the execution of the script. The shutdown functions and object destructors will always be executed even if exit[] function is called.
exit["This request is processed"];
...
PHP..
for those of you who think that using return in a script is the same as using exit note that: using return just exits the execution of the current script, exit the whole execution.
You can't break if statements, only loops like for or while. If this if is in a function, use 'return'.