exit, die, and return PHP

Like break, the exit statement offers a way to escape from execution, but the exit statement stops all execution. Not even text outside of PHP tags is sent to the browser. This is useful when an error occurs and it would be more harmful to continue executing code than to just abort. This is often the case when preparing database queries. If the SQL statement cannot be parsed, it makes no sense to try to execute it.

The die statement is similar to exit, except that it may be followed by an expression that will be sent to the browser just before aborting the script. Using the fact that subexpressions in an if statement are evaluated left to right and only as necessary, the idiom in is allowed. Notice the parentheses around the string to be printed when the open fails. They are required.

diom for Using the die Statement

$fp = fopen("somefile.txt", "r") OR die("Unable to open file");

If called outside of a function, the return statement stops execution of the current script and returns control to the script that made a call to include. That is, when a script uses the include function, the included script may return prematurely. If you use return in a script that was not invoked by include, the script will simply terminate as if exit were used.

We admit this is a strange concept, and it probably deserves to have its own name instead of se aring one with the statement for returning from functions. On the other hand, in certain special cases, it allows for tidy code.



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics