Php exec cannot return negative values in linux _ PHP Tutorial

Source: Internet
Author: User
Php exec cannot return negative values in linux. The exec function is normal in windows, but the return value cannot be negative in linux. Stringexec (string $ command [, array $ output [, int $ return_var]) The third exec function has no problem in windows, however, in linux, the return value cannot be negative.

String exec (string $ command [, array & $ output [, int & $ return_var])

In the third parameter, how can I not receive negative numbers ??
Here & $ return_var is the return value of the program. at first, my answer is a negative number.
We usually write this in c.

The code is as follows:

# Include
# Include
Int main ()
{
Printf ("^_^ n ");
Return-5;
}

This-5 is the return value, but it is written as 0 or 1.
Note: Many people write the main function as void main () in C code. this is actually not correct, so I won't go into details.
Compile the above code and run it in CMD. then the output result is displayed. Enter "echo % ERRORLEVEL %" and press enter to view the program return value. This % ERRORLEVEL % indicates the return status of the program. It can be a negative number in WIN. :

, Php call is also normal.

The code is as follows:
E: devphp535> php-r "exec('return.exe ', $ out, $ a); var_dump ($ );"
Int (-2)

However, in linux, it is always a positive number. it was initially suspected that it was a Permission problem. after chmod + x was used, the permission problem was ruled out.

The code is as follows:
Exec ("/home/wwwroot/test/rtest. out 2> & 1", $ out, $ );
Var_dump ($ out, $ );
Array (1) {[0] => string (3) "'''} int (251)

It seems to be 256 + return val. we can see that a negative number is actually returned, but it is converted to a positive number.
Then I read the source code in standard/exec. c, but I didn't find any clues. it was very strange. I suddenly thought I forgot a step. I forgot to read the OS value returned by the program.
Echo $? Displays the launch status of the last command.

The code is as follows:
-Bash-3.00 $ vi main. c
-Bash-3.00 $ gcc-o./mm main. c
-Bash-3.00 $ ll
Total 48
Drwxr-xr-x 3 www 4096 May 4 2011 2011
Drwxr-xr-x 6 www 4096 Jun 23 2011 eoc
-Rwxr-xr-x 1 www 7131 Feb 1 hello
-Rw-r -- 1 www 3 Feb 1 :51 hello. c
-Rw-r -- 1 www 99 Feb 1 :50 main. c
-Rwxr-xr-x 1 www 4714 Feb 1mm
Drwxr-xr-x 3 www 4096 Jun 24 2011 test
-Bash-3.00 $./mm
Pai_^
-Bash-3.00 $ echo $?
251
-Bash-3.00 $

In this way, you can see the value of exec returned to OS.
In linux, the returned value is of the unsigned type and returns a positive value. Therefore, it is also a positive value to pass to php. php is actually the value returned by the called exec.

Exec directory operations


2 down vote For greater control over how the child process will be executed, you can use the proc_open () function:

2 down vote

For greater control over how the child process will be executed, you can use the proc_open () function:

The code is as follows:

$ Cmd = 'Scripts/script. sh ';
$ Cwd = 'scripts ';

$ Spec = array (
// Can something more portable be 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 are not passing/dev/null like above, we shoshould close
// Our ends of any pipes to signal that we're re done. Otherwise
// The call to proc_close below may block indefinitely.
Foreach ($ pipes as $ pipe ){
@ Fclose ($ pipe );
}

// Will wait for the process to terminate
$ Exit_code = proc_close ($ ph );
If ($ exit_code! = 0 ){
// Child error
}

Bytes. String exec (string $ command [, array $ output [, int $ return_var]) 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.