我尝试了以下方法:
<?php
function shutdown_find_exit()
{
exit(50);
}
register_shutdown_function('shutdown_find_exit');
trigger_error("Fatal error", E_USER_ERROR);
?>
但是错误码为255。
我正在尝试返回错误码50。
我正在用下面的bash脚本检查它:
php tester.php
status=$?
echo Exit Code: ${status}
为任何感兴趣的人更新了基于接受的答案的代码:
<?php
function shutdown_find_exit()
{
exit('50');
}
register_shutdown_function('shutdown_find_exit');
trigger_error("Fatal error", E_USER_ERROR);
?>
和bash脚本
output="$(php tester.php)"
status=$?
echo Exit Code: ${status}
if [[ $output == *"50"* ]]
then
echo "Got 50";
fi
TL;DR:使用string类型,而不是int类型。
php exit( status )
如果status是字符串,该函数只打印状态在退出前。
如果status为整数,则该值将用作退出状态而不是印刷出来的。退出状态应该在0到254的范围内退出状态255是PHP保留的,不能使用。状态0用于成功终止程序。
注意:PHP>= 4.2.0不打印状态,如果它是一个整数
在bash中执行php脚本,将输出赋值给变量。
variable = $(/path/to/php -f $HOME/path/to/my.php)
例如:# just executing php, but capturing output then, prints it to console
status = $(/usr/bin/php -f $HOME/path/to/tester.php)
echo Exit Code: ${status}
看看这是不是你想做的:
trigger_error("Fatal error", shutdown_find_exit());