简体   繁体   English

PHP 从包含的脚本中退出(),退出父脚本?

[英]PHP exit() from within included script, exit parent script?

In PHP, if I use the include() or require() functions to start running code in another script, is there a way to terminate the parent script from within the child?在 PHP 中,如果我使用 include() 或 require() 函数开始在另一个脚本中运行代码,有没有办法从子脚本中终止父脚本?

So say I have this in parent.php:所以说我在parent.php中有这个:
require('child.php');

And this in child.php:这在 child.php 中:
exit();

Will that terminate just child.php, or parent.php as well?这会终止 child.php 还是 parent.php 吗?
Is there a way to terminate parent.php from within child.php, without adding any further code to parent.php?有没有办法从 child.php 中终止 parent.php,而不向 parent.php 添加任何进一步的代码?

It's for an elaborate custom 404 error page in PHP which detects whether it's been called by Apache using ErrorDocument, or whether a user has requested a page from our custom CMS which doesn't exist.它用于 PHP 中精心设计的自定义 404 错误页面,它检测 Apache 是否使用 ErrorDocument 调用了它,或者用户是否从我们的自定义 CMS 请求了一个不存在的页面。 If it's the latter, then I want to just require my 404.php (child) and output from that and not continue executing the parent.如果是后者,那么我想只需要我的 404.php (子)和 output ,而不是继续执行父。

Anyone know if it's possible to terminate a parent PHP script from within an included/required script?任何人都知道是否可以从包含/必需的脚本中终止父 PHP 脚本?

exit(); Will that terminate just child.php, or parent.php as well?这会终止 child.php 还是 parent.php 吗?

It will terminate the entire script.它将终止整个脚本。

If you don't want to do that, but return to the including script, you can return from within an include.如果您不想这样做,而是返回包含脚本,则可以从包含中return

You are looking for return ;您正在寻找return command.命令。 This will terminate execution of only the child.php, and parent.php will be processed after that.这将仅终止 child.php 的执行,然后处理 parent.php。

You can use return if you need to exist included file but continue from main script.如果您需要存在包含文件但从主脚本继续,您可以使用return

return返回

(PHP 4, PHP 5, PHP 7) (PHP 4,PHP 5,PHP 7)

return returns program control to the calling module.Execution resumes at the expression following the called module's invocation. return将程序控制权返回给调用模块。在被调用模块调用之后的表达式处继续执行。

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.如果从 function 中调用,return 语句将立即结束当前 function 的执行,并将其参数作为 function 调用的值返回。 return also ends the execution of an eval() statement or script file. return 还结束 eval() 语句或脚本文件的执行。

If called from the global scope, then execution of the current script file is ended.如果从全局 scope 调用,则结束当前脚本文件的执行。 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.如果从主脚本文件中调用 return,则脚本执行结束。 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如果当前脚本文件由 php.ini 中的 auto_prepend_file 或 auto_append_file 配置选项命名,则该脚本文件的执行结束

Anyone know if it's possible to terminate a parent PHP script from within an included/required script?任何人都知道是否可以从包含/必需的脚本中终止父 PHP 脚本?

You can use您可以使用

die();

to end the furthur execution of any script at any point.在任何时候结束任何脚本的进一步执行。 Use of Die puts an end to the parent scripts as well.使用 Die 也结束了父脚本。

die("End");

Will output "end".将 output “结束”。

Actually an exit;实际上是一个exit; line in your child.php will terminate current php process that means parent.php will be terminated well.child.php将终止当前 php 进程,这意味着parent.php将很好地终止。

You can also terminate an script by throwing an exception and catch it in one of the parent scripts.您还可以通过引发异常来终止脚本并在其中一个父脚本中捕获它。

This way you can control with precission which scripts to terminate and where to continue in the stack of "include / require" files.通过这种方式,您可以精确控制要终止的脚本以及在“include / require”文件堆栈中继续的位置。

die and exit will both terminate without prejudice. dieexit都将在不影响的情况下终止。 It is an application level command which cannot be caught or undone.这是一个应用程序级别的命令,不能被捕获或撤消。

Using exit() will stop script execution and terminate the child and all parents.使用exit()将停止脚本执行并终止孩子和所有父母。

You can try to throw an exception:您可以尝试抛出异常:

throw new Exception('An Error Ocurred');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM