How do you judge exec function success in PHP? _php instance

Source: Internet
Author: User

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.

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.