Open In App

Explain the Difference Between shell_exec() and exec() Functions

Last Updated : 12 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn about the shell_exec() & exec() functions in PHP. As we know that in order to execute a command in a system, we need the shell of the respective operating systems, but if we need to execute the same command with the help of a programming language like PHP, then we use these two functions. We use any of the two functions but they differ in terms of output, they will give after their successful execution. Please refer to PHP | shell_exec() vs exec() Function.

shell_exec() Function: 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 shell_exec is an alias for the backtick operator, for those used to *nix. If the command fails, it returns NULL and the values are not reliable for error checking. This function gets disabled when PHP runs in safe mode.

Syntax:

string shell_exec( $cmd )

Parameter: shell_exec() function passes only a single argument($cmd) as it holds the command that is to be executed.

Return value: it returns the executed command or NULL if an error occurs.

Example 1:

PHP




<?php
  
// Command 
$command = "DATE";
  
// Passing the command to the function
$cmd_output = shell_exec($command);
  
echo $cmd_output;
?>


Output

The current date is 08-07-2021. Enter the new date: (dd-mm-yy)

Example 2:

PHP




<?php
  
// Command 
$command = "TIME";
  
// Passing the command to the function
$cmd_output = shell_exec($command);
  
echo $cmd_output;
?>


Output:

The current time is: 21:05:01.82. Enter the new time: 

 

exec() Function: 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. It also returns NULL if no command runs properly. It differs in terms of the output it gives wrt shell_exec() function. It executes the command and gives all the output of that command in the form of an array and it also gives some extra info that we can use to check whether the command is successfully executed or not.

Syntax:

string exec( $command, $output, $return_var )

Parameters: This function will accept three parameters:

  • $command: used to hold the command which is to be executed.
  • $output: used to specify the array which will be filled with every line of output from the command.
  • $return_var: This parameter is present along with the output argument, then it returns the status of the executed command that will be written to this variable.

Return Value: Return the executed command.

Example 1:

PHP




<?php
  
// Command to be executed in the shell
$command = "DATE";
  
// Calling exec function 
exec($command, $output_array, $cmd_status);
  
echo "Command status - ".$cmd_status." <br><br>";
echo var_dump($output_array)
  
?>


Output:

Command status - 1

array(2) { [0]=> string(31) "The current date is: 08-07-2021" 
           [1]=> string(30) "Enter the new date: (dd-mm-yy)" } 

Example 2:

PHP




<?php
  
// Command to be executed in the shell
$command = "TIME";
  
// Calling exec function 
exec($command, $output_array, $cmd_status);
  
echo "Command status - ".$cmd_status." <br><br>";
echo var_dump($output_array)
  
?>


Output:

Command status - 1

array(2) { [0]=> string(32) "The current time is: 21:38:09.81" 
           [1]=> string(19) "Enter the new time:" }

Difference between shell_exec() and exec() function:

 

shell_exec()

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. It also returns NULL if no command runs properly.

2.

The shell_exec() function provides complete output as a string.

The exec() function can provide all output in the form of an array specified as the second parameter.

3.

The shell_exec() function can return null for both cases when an error occurs or the program produces no output.

The exec() function can return null only in the case when no command executes properly. 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads