Objective
To do a code release system, you need to use the exec function of PHP to execute Linux commands and git,svn commands, how to determine whether the exec function of PHP is executed successfully?
Solution
Write a php file to do the experiment:
The first parameter of the exec function is the command executed, the second parameter is the result of execution, and the third is the state of execution.
<?php
exec (' ls ', $log, $status);
Print_r ($log);
Print_r ($status);
Echo Php_eol;
Execute this PHP file:
Here $log, $status output as shown.
But $status is 0, giving people the feeling that execution failed, in fact not, that exec executed successfully .
Change this php file and give exec the first argument with a wrong command.
Such as:exec(‘lsaa',$log,$status).
Execute again, run the results as shown in figure:
There is a value here $status .
Then the proof of $status 0 indicates that exec execution was successful. This is not explicitly stated in the official PHP manual.
The final way to execute the command is as follows:
PHP exec Execute command PHP
Public Function Runlocalcommand ($command) {
$command = trim ($command);
$status = 1;
$log = ';
EXEC ($command. ' 2>&1 ', $log, $status);
Executed command
$this->command = $command;
State of execution
$this->status =! $status;
return $this->status;
}
Eliminates log records and other judgments.
Note here:
$this->status =! $status;
Return to the state of the time to take the opposite value!
Summarize
The above is in PHP to determine whether the exec function to execute the success of the details and instance code, help you to understand the depth of PHP development, I hope this article on the development of PHP to help you learn.