Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences of system(), exec() and shell_exec() in PHP?

It is possible to run an external command by three PHP functions of

system(); exec(); shell_exec(); 

but what are their differences? In spite of their specific applications, in most cases, the can be equally used. I am curious to know which is preferred one when they can be equally used. For example, for unzipping a file or compressing a folder (with tar command), which one is preferred (probably from performance point of view)?

UPDATE: In another question, I found a very useful link describing different aspects for these functions. I share the link here, as other may use to better understand security issues and other aspects.

like image 668
Googlebot Avatar asked May 31 '12 06:05

Googlebot


People also ask

What is exec() in PHP?

The exec() function is an inbuilt function in PHP which is used to execute an external program and returns the last line of the output. It also returns NULL if no command run properly. Syntax: string exec( $command, $output, $return_var )


1 Answers

exec — Execute an external program

system — Execute an external program and display the output

shell_exec — Execute command via shell and return the complete output as a string

so if you don't need the output, I would go with exec.

Further details:

  • http://php.net/manual/en/function.exec.php
  • http://php.net/manual/en/function.system.php
  • http://php.net/shell_exec
like image 171
Gavriel Avatar answered Oct 16 '22 21:10

Gavriel