PHP exec in Linux return value cannot be negative question _php tutorial

Source: Internet
Author: User
The EXEC function is not a problem in a Windows environment, but the return value in Linux cannot be negative.

String exec (string $command [, Array & $output [, int & $return _var]])

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 can really 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.

EXEC directory Operations


2down vote for greater control-over-the-child process would be-executed, you can use the Proc_open () function:

2 Down vote

For greater control-over-the-child process would be-executed, you can use the Proc_open () function:

The code is as follows Copy Code

$cmd = ' scripts/script.sh ';
$CWD = ' Scripts ';

$spec = Array (
Can something more portable is passed here instead of/dev/null?
0 = = Array (' file ', '/dev/null ', ' R '),
1 = = Array (' file ', '/dev/null ', ' W '),
2 = = Array (' file ', '/dev/null ', ' W '),
);

$ph = Proc_open ($cmd, $spec, $pipes, $CWD);
if ($ph = = = FALSE) {
Open error
}

If We is not a passing/dev/null like above, we should close
Our ends of any pipes to signal that we ' re do. Otherwise
The proc_close below may block indefinitely.
foreach ($pipes as $pipe) {
@fclose ($pipe);
}

Would wait for the process to terminate
$exit _code = Proc_close ($PH);
if ($exit _code!== 0) {
Child error
}

http://www.bkjia.com/PHPjc/632187.html www.bkjia.com true http://www.bkjia.com/PHPjc/632187.html techarticle The exec function is not a problem in a Windows environment, but the return value in Linux cannot be negative. String exec (string $command [, array $output [, int $return _var]]) The third parameter ...

  • 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.