Home > Backend Development > PHP Problem > What is the difference between system and exec in php

What is the difference between system and exec in php

青灯夜游
Release: 2023-03-05 15:14:01
Original
4328 people have browsed it

The difference between system and exec in php is: system executes an external program and displays the output, and it can output and return results; exec executes an external program and does not output results but returns the last line of the result.

What is the difference between system and exec in php

#To call external commands in PHP, you can use exec and system. So what are the differences between them? This article will introduce to you the difference between system and exec in PHP.

1. exec ---Execute external program

string exec ( string $command [, array &$output [, int &$return_var ]] )
Copy after login

$command: shell command to be executed

$output: The output of the shell command fills this array, and each line of output fills one element in the array. Please note that if the array already contains some elements, the exec() function will append the content to the end of the array. If you don't want to append at the end of the array, use the unset() function to reset the array before passing it to the exec() function.

##$return_var Return status after command execution, Command execution success value is 0

Return value: the last line output by the shell command

##ps: 2>&1 If exec is unsuccessful, one trick for debugging is to use a pipeline command. Using 2>&1, the command will output the error during shell execution to the $output variable , output this variable to analyze.

Example 1

(1) Where the code is located The structure of the index.php file

 (2 ) Code

$out = [34];
$res = exec('ls 2>&1',$out,$return_status);
var_dump($res);
echo '------';
var_dump($out);
echo '------';
var_dump($return_status);
Copy after login

(3) Execution result

zhangxueqing:demo playcrab$ php  ./1/index.php
/Users/playcrab/www/demo/1/index.php:10:
string(11) "webuploader"
------/Users/playcrab/www/demo/1/index.php:12:
array(10) {
  [0] =>
  int(34)
  [1] =>
  string(1) "1"
  [2] =>
  string(6) "1.html"
  [3] =>
  string(5) "1.php"
  [4] =>
  string(10) "client.php"
  [5] =>
  string(14) "design-pattern"
  [6] =>
  string(3) "img"
  [7] =>
  string(17) "jquery.blockUI.js"
  [8] =>
  string(10) "static.php"
  [9] =>
  string(11) "webuploader"
}
------/Users/playcrab/www/demo/1/index.php:14:
int(0)
Copy after login

2. system ---

Execute the external program and display the output

string system ( string $command [, int &$return_var ] )
Copy after login
$command The command to be executed

$return_var The return status after command execution,

The value is 0 indicating success

Return result:

    Successfully returns 0,
  • Failure (the command does not exist, etc.) Returns a non-0 value
  • 1. Sample code
$res = system('ls 2>&1',$return_status);var_dump($res);echo '------';var_dump($return_status);
Copy after login

2. Output results

##Summary:

system() function Much like in other languages, it executes the given command, prints and returns the results. The second parameter is optional and is used to get the status code after the command is executed.

The exec () function is similar to system(). It also executes the given command, but does not output the result, but returns the result. the last line of. Although it only returns the last line of the command result, using the second parameter array can get the complete result by appending the results line by line to the end of the array. So if the array is not empty, it is best to use unset() to clear it before calling it. Only when the second parameter is specified, the third parameter can be used to obtain the status code of command execution.

Recommended: "

PHP Video Tutorial

"

The above is the detailed content of What is the difference between system and exec in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template