Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

return

exit

die

Am am still on a PHP learning curb. When terminating a script, what is the difference between exit(), die(); and return;?:

  1. within the same file (Single script file)
  2. Within the child of an include
  3. Within the parent of an include
like image 648
Omar Avatar asked Dec 20 '12 08:12

Omar


People also ask

What is the difference between return and exit in PHP?

Exit halts all execution. A global return will halt the current script and return to the calling script (if there is one).

What does exit () do in PHP?

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.

Is exit and die the same?

There is no difference between die and exit, they are the same. "This language construct is equivalent to die()." "This language construct is equivalent to exit()." However, there is a small difference, i.e the amount of time it takes for the parser to return the token.

What is the purpose of the die () function?

Definition and Usage The die() function prints a message and exits the current script. This function is an alias of the exit() function.


3 Answers

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

like image 98
Jordi Kroon Avatar answered Oct 02 '22 04:10

Jordi Kroon


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.

like image 26
Geoffrey Hale Avatar answered Oct 02 '22 05:10

Geoffrey Hale


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); //
?>
like image 26
EngineerCoder Avatar answered Oct 02 '22 05:10

EngineerCoder