Check that the EXEC function of PHP is executed successfully

Source: Internet
Author: User
Tags php file

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?

Write a php file to do the experiment:

exec function parsing

EXEC syntax: string exec (String command, string [array], int [return_var]);
EXEC return Value: string
EXEC parameter description
command– the command to be executed
array– is the output value
return_var– is the return value of 0 or 1, and if 0 is returned, the execution succeeds, and the return 1 fails.

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 in $status here.

Then proving that $status is 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);
Commands that have been executed
$this->command = $command;
Status 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!

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.