PHP exec return value in Linux cannot be negative _php tutorial

Source: Internet
Author: User
PHP exec return value in Linux can not be negative if you need a friend to refer to.

Let's start with a look at how the EXEC function is used

EXEC ()
Prototype:

The code is as follows Copy Code
String exec (String command [, string array [, int return_var]])

The exec () function, like system (), also executes the given command, but does not output the result, but instead returns the last line of the result. Although it returns only the last line of the command result, the second parameter array gives the complete result by appending the result line to the end of the array. So if the array is not empty, it is best to clear it with unset () before calling it. The third parameter can be used to obtain the status code of the command execution only if the second parameter is specified.
Example:

The code is as follows Copy Code
EXEC ("/bin/ls-l");
EXEC ("/bin/ls-l", $res);
EXEC ("/bin/ls-l", $res, $RC);

Third parameter, how can not receive negative numbers??
Here & $return _var is the program return value, at first my answer is can be negative.
Usually in C, we write that.

The code is as follows Copy Code
#include
#include
int main ()
{
printf ("^_^ n");
return-5;
}

This-5 is the return value, but it is customary to write 0 or 1.
Note: In many people's C code, the main function is written in void Main (), which is actually not true, and the details are not mentioned.
After compiling the above code, run to CMD and you will see the output. Then, enter "Echo%errorlevel%" and the return value of the program can be seen. This%errorlevel% represents the return status of the program. Under win, it's really possible to be negative.

, the PHP call is also normal.

The code is as follows Copy Code
E:devphp535>php-r "exec (' Return.exe ', $out, $a); Var_dump ($a);"

Int (-2)
But to Linux, always a positive number, just beginning to suspect that the permissions issue, with the chmod +x, the issue of the exclusion of permissions.

The code is as follows Copy Code
EXEC ("/home/wwwroot/test/rtest.out 2>&1", $out, $a);
Var_dump ($out, $a);
Array (1) {[0]=> string (3) "^_^"} int (251)

It looks like 256+return Val, and you can see that it actually returns a negative number, but is converted to a positive number.
Then saw the next standard/exec.c in the source code, did not find what clues, dry to very strange, suddenly thought he forgot a step. Forgot to see the value that the program returned to the OS.
Can I use echo $? Displays the launch status of the last command.

copy code
-bash-3.00$ vi main.c
-bash-3.00$ gcc-o./mm MAIN.C
-bash-3.00$ ll
Total (
drwxr-xr-x 3 www. 4096 4)
Drwxr-xr-x 6 www www 4096 June 23 2011 EOC
-rwxr-xr-x 1 www www 7131 1 12:47 hello
-rw-r--r--1 www www 3 Feb 1 12:51 hello.c
-rw-r--r--1 www www 1 12:50 main.c
-rwxr-xr-x 1 www www 4714 Feb 1 12:51 mm
drwxr-xr-x 3 www www 4096 june-Test
-bash-3 .00$./mm
^_^
-bash-3.00$ echo $?
251
-bash-3.00$

This allows you to see how much value exec returns to the OS.
Under Linux, this return value is an unsigned type and returns a positive number, so it is positive for PHP, and PHP is actually the value returned by the call exec.

Out, perhaps Linux is so stipulated, the program in the return value of Thai is 1~255 interval, and I do not know this rule. However, it is not yet determined whether this is the rule in Linux. Although the problem is very painful, the meaning is not big, but can also be left here for people who encounter similar problems to reference. But there's no need to delve into it. If anyone really wants to get this straight, look at the shell.

http://www.bkjia.com/PHPjc/632189.html www.bkjia.com true http://www.bkjia.com/PHPjc/632189.html techarticle PHP exec return value in Linux can not be negative if you need a friend to refer to. Let's take a look at the use of the EXEC function in the EXEC () prototype: The code follows the copy code St ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.