Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP exec() vs system() vs passthru()

Tags:

command

php

exec

What are the differences?

Is there a specific situation or reason for each function? If yes, can you give some examples of those situations?

PHP.net says that they are used to execute external programs. see reference From the examples I see, I don't see any obvious difference.

If I were to simply run a script (bash or python), which function do you recommend me to use?

like image 302
codingbear Avatar asked Apr 09 '09 04:04

codingbear


People also ask

What is passthru in PHP?

The passthru() function is similar to the exec() function in that it executes a command . This function should be used in place of exec() or system() when the output from the Unix command is binary data which needs to be passed directly back to the browser.

What is system() in PHP?

system() is just like the C version of the function in that it executes the given command and outputs the result. The system() call also tries to automatically flush the web server's output buffer after each line of output if PHP is running as a server module.

What is the difference between exec and Shell_exec?

1. The shell_exec() function is an inbuilt function in PHP that is used to execute the commands via shell and return the complete output as a string. The exec() function is an inbuilt function in PHP that is used to execute an external program and returns the last line of the output.

Which function in PHP is used for OS command execution?

exec function: This function executes a system command, stores the output in an array, and returns the last line of the output.


1 Answers

They have slightly different purposes.

  • exec() is for calling a system command, and perhaps dealing with the output yourself.
  • system() is for executing a system command and immediately displaying the output - presumably text.
  • passthru() is for executing a system command which you wish the raw return from - presumably something binary.

Regardless, I suggest you not use any of them. They all produce highly unportable code.

like image 56
Kalium Avatar answered Oct 13 '22 23:10

Kalium