I am running a typical php-engined ajax webpage. I use echo to return a html string from the php code. My question is, if I have some other code after the echo, will those code get executed? Or echo behaves similar to exit, which immediately return and stop running the php code? Thanks.
No, echo in no way exits, you normally have more than one echo in a script. exit does take a string argument that it will output before exiting, however, so you can do:
exit("your string here");
and it will output the string and exit
No, echo would not. To exit after echoing things, you'd say
echo "Dear me, good bye!"; exit();
echo will simply return text to ajax javascript part; however the code after or before echo/echos will execute
No. PHP scripts are rendered in their entirety unless you explicitly exit them. ANY output on a script will be passed back to the ajax function if it was called through ajax.
echo 'This gets outputted<br />';
echo 'As does this';
If you must use a single file and you want your script to exit after performing ajax request with out having to add extra vars to your ajax url or evaluate vars to exit, i would suggest creating a function that performs your ajax, have the function return true on success, then do:
if(ajaxFunction($paramOne, $paramTwo)){exit();}
Related
What is the reason why you need to use die(); to end ajax requests? I read that it's recommended to use die() or exit() to terminate the script when its sole purpose is meant for AJAX call?
Is this recommended for my script when the output is the very last thing in the script? Because my index.php file loads the environment, gets the content from controller then the very last thing is using echo to output the data. It's either JSON encoded data for ajax, or HTML content for non ajax. I could add the die at the end by checking if it's an ajax call, but I don't understand why I would need it here.
// index.php
// Load environment
require '../load.php';
// Handle request
$controller = new App\Controllers\Controller();
$content = $controller->doRequest();
// Output the content
echo $content;
// Terminate script with die() for AJAX requests (is this needed?)
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
die();
}
If you use one of those php-frameworks which can output additional data by themselves -- then you need to use exit
Also do not forget to DO NOT use ?> at the end of your script. You may leave empty line after php closing tag so your web-server will add this empty line to answer if you do not use exit
Also, do not forget about register_shutdown_function -- it will run immediatly after exit, so exit is the good way to prevent adding any other shutdown functions after echo $content;
You may not use exit in this concrete script, but also don't forget to remove ?> too
I have a function in one of my phpunit tests that does this:
echo "Order id: ".$order->id."\n";
$email = $this->getOrderEmail($order->id);
while($email === null) {
echo "Sleeping for order id ".$order->id."\n";
$this->saything();
sleep(1);
$email = $this->getOrderEmail($order->id);
}
echo "Got email: ".$email->MessageID."\n";
It runs the same code for several orders. The saything() method just echoes time().
When I run it, the first time it executes the code, it echoes correctly. When it runs it for the second order however, it echoes the "Got email", so I know it has exited the loop, and then it appears to stop processing.
When I use xdebug to trace the execution, I see that it is somehow still in the loop - it calls saything, which calls time(), and is supposed to echo it (literally: echo time();) - but doesn't. So the time() call is happening, but the echo() isn't. It hits the sleep, then the getOrderEmail, which returns null, triggering the loop to repeat but NOT echoing the "sleeping for..." message.
So I can clearly see in the xdebug trace that it is stuck in an infinite loop, but nothing is coming through to stdout! Why not? Could there be a bug in php somewhere? I've tried calling flush(), but that did nothing. How do I debug this? Also: How can it still be in the loop, when it has already executed the line following the loop? I've checked the process list, and it's definitely not forking, so I can't think of anyway it can exit a loop, while still being in the loop, and call echo without echoing anything.
I am aware that the problem is probablysomewhere else in the code, but it's way too big to post the whole thing here. Any ideas on how I can figure this out?
From PHP Manual:
"flush() may not be able to override the buffering scheme of your web server and it has no effect on any client-side buffering in the browser. It also doesn't affect PHP's userspace output buffering mechanism. This means you will have to call both ob_flush() and flush() to flush the ob output buffers if you are using those."
You should try using ob_flush();
According to http://www.w3schools.com/php/func_misc_exit.asp the php exit function prints a message and exits the current script. Where does the message get printed?
The exit() function outputs to standard out. Typically this is sent right back to the client (Browser), but a lot of things might interfere with that: Bad Output buffering or redirection e.g.
If you pass it a string, it gets echoed to the screen (just like if you were to use echo).
If you pass it an int, it's not displayed, but instead "returned" as the program's return code.
It outputs it to the browser, you see a plain page with the exit message, or if previous output was printed then it appends this to the bottom.
jQuery.ajax() is "a function to be called when the request finishes". Suppose I'm making an ajax request to ajax.php:
<?php
echo 'complete';
some_functions_that_echo_nothing();
?>
Will the complete have to wait for some_functions_that_echo_nothing()? If so, is there a way to make the complete{} occur right after the echo and still have the ajax.php run through till the end?
Try calling HttpResponse::send();
I'm guessing you want to output "Complete" and let that function run in background, since it's very slow.
In that case put this function in a separate file. Let it be proc.php and use this instead:
<?php
echo "Complete';
exec ("/usr/bin/php proc.php >/dev/null &");
?>
That will return right away and fire the proc.php file to run on background. Of course it won't be able to output it's return to the user, so it should mail the user when he's done, or do his own persistence.
EDIT: ALWAYS take grea care of what you put inside exec statements. Never put user inputs into it if you are not 100% sure you are sanitizing it very carefully. Even so, you really don't have a good reason to use User input into an exec call.
in PHP Does die() gives anything in return when we use it?
In PHP the function die() just quit running the script and prints out the argument (if there's any).
http://php.net/die
Obviously, die() or its equivalent exit() don't return anything to the script itself; to be precise, this code doesn't make much sense:
if (die())) {
echo 'are we dead yet?';
}
However, depending on what you pass as the (optional) argument of die() or exit(), it does return something to the caller, i.e. the command that caused your script to run. Its practical use is usually limited to the cli SAPI though, when you call the script from a command line using php /path/to/script.php.
Observe:
die('goodbye cruel world');
This code would print goodbye cruel world and then return an exit status code of 0, signalling to the caller that the process terminated normally.
Another example:
die(1);
When you pass an integer value instead of a string, nothing is printed and the exit status code will be 1, signalling to the caller that the process didn't terminate normally.
Lastly, die() without any arguments is the same as die(0).
The exit status of a process can be changed to signal different kinds of errors that may have occurred, e.g. 1 means general error, 2 means invalid username, etc.
It is the same as exit() and according to documentation it returns nothing
It does not return. The script is terminated and nothing else is executed.
There's no reason to return something in die/exit. This function terminates php interpreter process inside and returns exit-code to shell. So after calling die() there is no script execution as far as there is no interpreter process which executes the script and that's why there is no way to handle function's return.