3.5. exit, die, and return

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 are evaluated according to precedence and associativity, and given the short-circuit nature of the logical operators, the ...

Get Core PHP Programming, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.