Asked 7 years ago
5 Oct 2016
Views 1321
sarah

sarah posted

what is differnce between exec() and system() and passthru() in PHP

i want to know what is diffrence between exec() and system() and passthru() in PHP ?
because sometime i want to run some system command , i mostly use exec , but dont which one is good to use to run system command in webservers(use both : linux or windows) , so advise me
web-api

web-api
answered Aug 19 '21 00:00

let discuss first what is exec() and system() and passthru() in PHP and how it works ? , so easily get what is differnce between exec() and system() and passthru() in PHP
i want to know what is diffrence between exec() and system() and passthru() in PHP ?

1. exec() function
exec() — run command
syntax ::
exec(string $command, array &$output = null, int &$result_code = null): string|false
here
$command parmeter is the string command to run
$output parmeter is hold $output which is array
$result_code is hold result code which is integer

exec() return String or False


2 . system()
system() — can run external program and display the output


syntax ::
system(string $command, int &$result_code = null): string|false
here
$command parmeter is the string command to run
$result_code is hold result code which is integer

system() return String or False


3 . passthru()
passthru() — Execute an external program and display raw output


syntax ::
passthru(string $command, int &$return_var = ?): void
here
$command parmeter is the string command to run
$return_var is for return status of the Unix command present

passthru() return nothing and output from the command passed directly back to the browser or to the terminal without any interference


so first big difference between passthru() vs exec() and system() is that passthru() is pass output from the command passed directly back to the browser or to the terminal without any interference in PHP
Post Answer