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.
#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 ]] )
$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);
(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)2. system ---
Execute the external program and display the outputstring system ( string $command [, int &$return_var ] )
$command The command to be executed
$return_var The return status after command execution,
The value is 0 indicating successReturn 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);
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!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1
Easy-to-use and free code editor

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)