PHP die () from an included page, not dying from the main page

Is there a way to use the die() function to stop the execution of the PHP instructions on the page included in another page, but to continue the execution of the PHP instructions on the page on which the file containing the die() function was included?

+4
source share
3 answers

use return; in the attached file. This will stop it, including execution. It works as a function. You can also return a value from your included file

+9
source

Not. die is an alias for exit , which immediately stops script execution.

But you can use return , which does exactly what you want:

When called from a global scope, execution of the current script file ends. If the current script file was include() ed or require() ed, then control is transferred back to the calling file. Also, if the current script file was include() ed, then the value specified in return() would be returned as the value of the include() call. If return() is called from the main script file, then the script terminates.

As stated in the excerpt from the PHP documentation , you can even use it to return the return code / return value from include:

 $include_retval = include('file_like_function.php'); if ($include_retval) { die("include returned error code: " . $include_retval); } 
+9
source

Not. Instead, could you use try blocks?

  try { include $file; } catch (Exception $e) { // Whatever } 

And throw an exception where you would use die() in $file .

0
source

Source: https://habr.com/ru/post/1403240/


All Articles