what does PHP die() return - php

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.

Related

PHP - Should I add the die() function at the end of every php script? [duplicate]

In a php script I have some test and after the script the html page.
When a test fail i call die("Test 1 failed");
If no test fail the php script reach the end ?> and then load the html code after the php script.
Is this a good procedure? Or I need to write die() or exit() before the end of php script?
No you don't have to write that and this is not best practice. If the script reaches the end without fatal errros it will exit.
If this means "testing" for you, you're wrong. Testing should be done using unit tests. For php there is phpunit. Give it a try, that's the proper way of testing your code.
Edit: As CompuChip says in a comment, the only useful use case for exit is when you're writing a php based shell script that should return an error code. See the parameter section of the documentation for the exit() function.
You should never be using die() or exit in your production PHP scripts except in very specific cases. Instead, re-work your code paths to simply show an error message to the user rather than exiting the script early.
No you don't need that, but when writing console PHP scripts, you might want to check with for example Bash if the script completed everything in the right way. That's when you use exit() or die()
Is the die() or exit() function needed in the end of a php script?
No, PHP will end the script itself. If the script is an included file (called from another file) then it will end script in the included file and then continue with any code in the original file after where you included (if there is any code).
So you put die() or exit() where ever you want or need it.
For testing, put it after each block of code you test. I use them in some parts of testing if I just want PHP to show me something then stop, such as print out an array to make sure it's being constructed correctly etc.
eg:
print_r($array);
exit();
For other code tests, I sometimes just echo "Section A worked", etc, such as within if/else. If I want to know if a particular part of code is working or if some criteria is being met or not (basically, it lets you trace where PHP itself is going within your code).
All that said, don't use die() or exit() in production code. You should use a more friendly and controlled messaging setup. For security reasons and visual, as you could potentially give them some info like "ERROR Failed to load SomethingSecret". Also it doesn't look pretty when you page only half loads and then puts out an on screen error message which likely means nothing to the end user.
Have a read through this:
PHP Error handling: die() Vs trigger_error() Vs throw Exception
No !
This is not recommanded to use it
Use trigger_error or error_log to log the tests in your error.log. Then check it.
No you don't have to use these functions at the end of the script, because it exists anyway at the end of the script.
No need to put a die or an exit at the end of the scipt.
But you may use exit to terminate your script with a specific exit code (by default it's 0).
E.g
$ php -r "/* does nothing */;"
$ echo $?
0
$ php -r "exit(123);"
$ echo $?
123
http://php.net/exit
From the documentation:
The link to the server will be closed as soon as the execution of the
script ends, unless it's closed earlier by explicitly calling
mysql_close().
https://secure.php.net/function.mysql-connect
Nope, you don't need to call die() or exit(0 if you have another code to run, like you HTML code

How to stop PHP code execution?

Is there a way to immediately stop PHP code execution?
I am aware of exit but it clearly states:
Terminates execution of the script. Shutdown functions and object destructors will always be executed even if exit is called.
So what I want to achieve is to stop the PHP code execution exactly when I call exit or whatever.
Any help?
Edit: After Jenson's answer
Trial 1:
function newExit() {
__halt_compiler();
}
echo "start";
newExit();
echo "you should not see this";
Shows Fatal error: __HALT_COMPILER() can only be used from the outermost scope in which was pretty expected.
Trial 2:
function newExit() {
include 'e.php';
}
echo "start";
newExit();
echo "you should not see this";
e.php just contains __halt_compiler();
This shows startyou should not see this
Edit: Why I want to do this?
I am working on an application that includes a proprietary library (required through virtual host config file to which I don't have access) that comes as encrypted code. Is a sort of monitoring library for security purpose. One of it's behaviours is that it registers some shutdown functions that log the instance status (it saves stats to a database)
What I want to do is to disable this logging for some specific conditions based on (remote IP)
Please see the following information from user Pekka 웃
According to the manual, destructors are executed even if the script gets terminated using die() or exit():
The destructor will be called even if script execution is stopped using exit(). Calling exit() in a destructor will prevent the remaining shutdown routines from executing.
According to this PHP: destructor vs register_shutdown_function, the destructor does not get executed when PHP's execution time limit is reached (Confirmed on Apache 2, PHP 5.2 on Windows 7).
The destructor also does not get executed when the script terminates because the memory limit was reached. (Just tested)
The destructor does get executed on fatal errors (Just tested) Update: The OP can't confirm this - there seem to be fatal errors where things are different
It does not get executed on parse errors (because the whole script won't be interpreted)
The destructor will certainly not be executed if the server process crashes or some other exception out of PHP's control occurs.
Referenced in this question
Are there any instances when the destructor in PHP is NOT called?
whats wrong with return ?
echo "you will see this";
return;
echo "you will not see this";
You can use __halt_compiler function which will Halt the compiler execution
http://www.php.net/manual/en/function.halt-compiler.php
You could try to kill the PHP process:
exec('kill -9 ' . getmypid());
Apart from the obvious die() and exit(), this also works:
<?php
echo "start";
__halt_compiler();
echo "you should not see this";
?>
I'm not sure you understand what "exit" states
Terminates execution of the script. Shutdown functions and object destructors will always be executed even if exit is called.
It's normal to do that, it must clear it's memmory of all the variables and functions you called before. Not doing this would mean your memmory would remain stuck and ocuppied in your RAM, and if this would happen several times you would need to reboot and flush your RAM in order to have any left.
or try
trigger_error('Die', E_ERROR);

Is the die() or exit() function needed in the end of a php script?

In a php script I have some test and after the script the html page.
When a test fail i call die("Test 1 failed");
If no test fail the php script reach the end ?> and then load the html code after the php script.
Is this a good procedure? Or I need to write die() or exit() before the end of php script?
No you don't have to write that and this is not best practice. If the script reaches the end without fatal errros it will exit.
If this means "testing" for you, you're wrong. Testing should be done using unit tests. For php there is phpunit. Give it a try, that's the proper way of testing your code.
Edit: As CompuChip says in a comment, the only useful use case for exit is when you're writing a php based shell script that should return an error code. See the parameter section of the documentation for the exit() function.
You should never be using die() or exit in your production PHP scripts except in very specific cases. Instead, re-work your code paths to simply show an error message to the user rather than exiting the script early.
No you don't need that, but when writing console PHP scripts, you might want to check with for example Bash if the script completed everything in the right way. That's when you use exit() or die()
Is the die() or exit() function needed in the end of a php script?
No, PHP will end the script itself. If the script is an included file (called from another file) then it will end script in the included file and then continue with any code in the original file after where you included (if there is any code).
So you put die() or exit() where ever you want or need it.
For testing, put it after each block of code you test. I use them in some parts of testing if I just want PHP to show me something then stop, such as print out an array to make sure it's being constructed correctly etc.
eg:
print_r($array);
exit();
For other code tests, I sometimes just echo "Section A worked", etc, such as within if/else. If I want to know if a particular part of code is working or if some criteria is being met or not (basically, it lets you trace where PHP itself is going within your code).
All that said, don't use die() or exit() in production code. You should use a more friendly and controlled messaging setup. For security reasons and visual, as you could potentially give them some info like "ERROR Failed to load SomethingSecret". Also it doesn't look pretty when you page only half loads and then puts out an on screen error message which likely means nothing to the end user.
Have a read through this:
PHP Error handling: die() Vs trigger_error() Vs throw Exception
No !
This is not recommanded to use it
Use trigger_error or error_log to log the tests in your error.log. Then check it.
No you don't have to use these functions at the end of the script, because it exists anyway at the end of the script.
No need to put a die or an exit at the end of the scipt.
But you may use exit to terminate your script with a specific exit code (by default it's 0).
E.g
$ php -r "/* does nothing */;"
$ echo $?
0
$ php -r "exit(123);"
$ echo $?
123
http://php.net/exit
From the documentation:
The link to the server will be closed as soon as the execution of the
script ends, unless it's closed earlier by explicitly calling
mysql_close().
https://secure.php.net/function.mysql-connect
Nope, you don't need to call die() or exit(0 if you have another code to run, like you HTML code

PHP: What is the difference between exit(), die() and return; within "self" and included files?

Am am still on a PHP learning curb. When terminating a script, what is the difference between exit(), die(); and return;?:
within the same file (Single script file)
Within the child of an include
Within the parent of an include
Return returns a value. This can be anything and is meant for functions.
What are the differences in die() and exit() in PHP?
http://php.net/manual/en/function.return.php
die and exit (equivalent functions)
Terminates execution of the script.
return
Returns program control to the calling module. Execution resumes at
the statement following the called module's invocation.
If called from within a function, the return statement immediately
ends execution of the current function, and returns its argument as
the value of the function call. return also ends the execution of an
eval() statement or script file.
If called from the global scope, then execution of the current script
file is ended. If the current script file was included or required,
then control is passed back to the calling file. Furthermore, if the
current script file was included, then the value given to return will
be returned as the value of the include call. If return is called from
within the main script file, then script execution ends. If the
current script file was named by the auto_prepend_file or
auto_append_file configuration options in php.ini, then that script
file's execution is ended.
die vs exit
The difference between die() and exit() in PHP is their origin.
exit() is from exit() in C.
die() is from die in Perl.
PHP Manual
PHP Manual for die:
This language construct is equivalent to exit().
PHP Manual for exit:
Note: This language construct is equivalent to die().
PHP Manual for List of Function Aliases:
die is an alias for master function exit()
DIFFERENT IN OTHER LANGUAGES
die() and exit() are different in other languages but in PHP they are identical.
From Yet another PHP rant:
...As a C and Perl coder, I was ready to answer, "Why, exit() just bails
off the program with a numeric exit status, while die() prints out the
error message to stderr and exits with EXIT_FAILURE status." But then
I remembered we're in messy-syntax-land of PHP.
In PHP, exit() and die() are identical.
The designers obviously thought "Hmm, let's borrow exit() from C. And Perl
folks probably will like it if we take die() as is from Perl too.
Oops! We have two exit functions now! Let's make it so that they both
can take a string or integer as an argument and make them identical!"
The end result is that this didn't really make things any "easier",
just more confusing. C and Perl coders will continue to use exit() to
toss an integer exit value only, and die() to toss an error message
and exit with a failure. Newbies and PHP-as-a-first-language people
will probably wonder "umm, two exit functions, which one should I
use?" The manual doesn't explain why there's exit() and die().
In general, PHP has a lot of weird redundancy like this - it tries to
be friendly to people who come from different language backgrounds,
but while doing so, it creates confusing redundancy.
Return is returns a value (char,int,string,array...) and exit from function.
From php manual :
Note: This language construct is equivalent to die().
But still there are difference between die and exit :
Using die() you can post a string : die("An error occurred");
Same result with using exit()
<?php
echo("An error occurred <br>");
exit(0);
?>
OR if you are cli or unix shell :
Using PHP on the command line, die("An error occurred") simply prints "An error occurred" to STDOUT and terminates the program with a normal exit code of 0.
<?php
fwrite(STDERR, "An error occurred \n");
exit(0); //
?>

Use of the parameter 'status' to exit

According to the documentation to exit:
If status is an integer, that value will be used as the exit status and not printed.
This made ​​me very confused. What is the difference between a exit(); and a exit(1);? What are the use cases? How should I choose? On what occasions? How php manages this state?
There's a numeric return code that can be read by the OS/the shell/the process that has invoked your PHP script. See: http://en.wikipedia.org/wiki/Exit_status
The difference between exit(); and exit(1); is, that the former sets the exit status of the process executing your PHP script to 0 and the latter sets it to 1.
An exit status of 0 usually means that the process finished sucessfully. No error occurred.
An exit status of 1 to 254 usually is used to signal that the process was aborted because some kind of error occurred. What error a specific exit status means is up to your PHP script.
Parent processes can use the exit statuses returned by child processes to decide how to continue, e.g. whether they should exit too, or retry, or execute another child process, or whatever.
I believe it's only useful if the script is being executed from the command line or another program is executing it, through the web server it's pointless.
it like the return code in C++, when usually the program returns 0 when everything is done successfully, negative when some error has occurred and positive number depending where that code can be used.
but in general as it is already said in previous answers, if the script won't be called from another script or program it doesn't make much sense what to return

Categories