【问题标题】:Is the exit status in PHP only useful when running from the command line?PHP中的退出状态是否仅在从命令行运行时有用?
【发布时间】:2025-12-20 02:50:11
【问题描述】:

我只是 reading the docs for PHP's exit 构造。

它说您可以传递一个整数 (0-254),它将成为退出代码...

exit(5);

这仅在 CLI 下从 PHP 运行时有用吗? Apache 可以将错误代码用于任何事情吗?正常运行的PHP会不会总是使用退出码0

谢谢

【问题讨论】:

    标签: php command-line


    【解决方案1】:

    虽然几乎不是权威答案,但我不知道它(将整数传递给exit)在 CLI 环境之外服务的任何目的。 Web 服务器传统上只报告 HTTP 状态代码,没有任何理由让它们在别处寻找状态代码。

    您可以在 sapi 目录中查看 PHP 的源代码。例如,在 php_cli.c 中,您会在文件末尾附近看到 exit(exit_status);。我假设通用 cgi 接口也使用它。我怀疑任何 Web 服务器接口都使用它。

    【讨论】:

    • 我能想到的更接近的是apache_note('exist_status', 5); exit ...在那里你可以得到exist_status的apache变量