PHP exec() 返回值是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9449825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the PHP exec() return value?
提问by burger
I am trying to use the PHP exec() function.
我正在尝试使用 PHP exec() 函数。
If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.
如果 return_var 参数与输出参数一起存在,则执行命令的返回状态将写入此变量。
If the execution was successful, it's 0. However, if there is an error, it can be a multitude of other integers. I can't seem to find anywhere what those integers correspond to. How should I interpret the integer that I get?
如果执行成功,它是 0。但是,如果有错误,它可以是许多其他整数。我似乎无法在任何地方找到这些整数对应的内容。我应该如何解释我得到的整数?
Update:
更新:
I really should have specified this originally, but I am executing another PHP script. Unlike rsync, which has exit values on its man page, I can't find an equivalent for PHP.
我真的应该最初指定这个,但我正在执行另一个 PHP 脚本。与手册页上有退出值的 rsync 不同,我找不到 PHP 的等效项。
So what I am doing is something like:
所以我在做什么是这样的:
$rv = exec('php file.php', $out, $rv);
采纳答案by Roadmaster
The return value is dependent on the process/program that you ran with exec. For instance, if you ran grep:
返回值取决于您使用 exec 运行的进程/程序。例如,如果您运行 grep:
The exit status is 0 if selected lines are found, and 1 if not found. If an error occurred the exit status is 2. (Note: POSIX error handling code should check for '2' or greater.)
如果找到选定的行,退出状态为 0,如果未找到,则退出状态为 1。如果发生错误,退出状态为 2。(注意:POSIX 错误处理代码应检查 '2' 或更大的值。)
rsync has about 20 different error exit codes, all painstakingly explained in the man page:
rsync 有大约 20 种不同的错误退出代码,所有这些都在手册页中详细解释:
http://linux.die.net/man/1/rsync
http://linux.die.net/man/1/rsync
so yes, it's program-dependant :)
所以是的,它取决于程序:)
Even if you're running PHP script, the exit value depends on your program itself. By default php scripts will exit with 0. If you use the exit function you can return different exit codes:
即使您正在运行 PHP 脚本,退出值也取决于您的程序本身。默认情况下,php 脚本将以 0 退出。如果您使用 exit 函数,您可以返回不同的退出代码:
http://php.net/manual/en/function.exit.php
http://php.net/manual/en/function.exit.php
If you want to experimentally determine what your php program exits, call it on the command line:
如果你想通过实验确定你的 php 程序退出了什么,在命令行上调用它:
php file.php
then do
然后做
echo $?
this will show you the exit value of your php script.
这将显示您的 php 脚本的退出值。
回答by freedev
IMHO, before use exec() function better set output and return_var parameters and read return code execution by return_var. Don't rely on exec() return value.
恕我直言,在使用 exec() 函数之前最好设置 output 和 return_var 参数并通过 return_var 读取返回代码执行。不要依赖 exec() 返回值。
回答by Ed Heal
Look up the manual page for the command that you are executing. This value has nothing to do with PHP but the actual command.
查找您正在执行的命令的手册页。该值与 PHP 无关,与实际命令无关。