search
HomeBackend DevelopmentPHP ProblemWhat is the difference between exec and system in php

The difference between exec and system in php: system executes an external program and displays the output, it can output and return results; exec executes an external program without outputting results but returning the last line of the result, but if you add a second Parameter array, you can also get the complete result.

What is the difference between exec and system in php

Recommended: "PHP Video Tutorial"

To call external commands in PHP, you can use exec and system Implementation:

system() ---Execute the external program and display the output

Prototype:

string system (string command [, int return_var])

system( ) function is very similar to that in other languages. It executes a given command, outputs and returns the result. The second parameter is optional and is used to get the status code after the command is executed.

Return result

Success returns 0,

Failure (the command does not exist, etc.) Returns a non-0 value

##exec( ) ---Execute external program

Prototype:

string exec (string command [, string array [, int return_var]])

The exec () function is similar to system() and also executes specified command, but does not output the result, but returns the last line of the result. 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.

##Example:

exec("/bin/ls -l"); 
exec("/bin/ls -l", $res); 
exec("/bin/ls -l", $res, $rc);
Related recommendations:

php training

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

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment