Explain the Difference Between shell_exec() and exec() Functions
Last Updated :
12 Aug, 2021
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.Â
|
Similar Reads
What is the difference between fopen() and fclose() functions in PHP ?
In this article, we are going to discuss the differences between fopen() and fclose() functions. fopen() function: This function helps users to open a file or a web URL. It mainly accepts two parameters - One is $file that specifies the file or an URL to be opened and the other is $mode that specifi
2 min read
Difference between die() and exit() functions in PHP
PHP exit() Function: In PHP, the exit() function prints a message and exits the application. It's often used to print a different message in the event of a mistake. Use exit() when there is not an error and have to stop the execution. Syntax: exit("Message goes here"); or exit(); Example: exit("This
2 min read
Perl - Difference between Functions and Subroutines
In Perl, when a code-script gets bigger in size i.e. contains hundreds of lines of code, it becomes difficult to manage such a code-script. To avoid this difficulty, Perl provides its users with the concept of Functions and subroutines. Functions and subroutines break up complex/large amounts of cod
4 min read
Difference between Shell and Kernel
In computing, the operating system (OS) serves as the fundamental layer that bridges the gap between computer hardware and the user. Two critical components of an operating system are the kernel and the shell. Understanding the relationship between these two components is fundamental to grasping how
4 min read
Difference between Function and Procedure
Pre-requisites: What is SQL? Structured Query Language is a computer language that we use to interact with a relational database.SQL is a tool for organizing, managing, and retrieving archived data from a computer database. In this article, we will see the difference between Function and Procedure.
3 min read
What is the difference between == and === in PHP ?
In this article, we will discuss the differences between '==' and '===' operators in PHP. Both are comparison operators used to compare two or more values. == Operator: This operator is used to check the given values are equal or not. If yes, it returns true, otherwise it returns false. Syntax: oper
2 min read
Difference between Static and Friend Function in C++
Static Function: It is basically a member function that can be called even when the object of the class is not initialized. These functions are associated with any object and are used to maintain a single copy of the class member function across different objects of the class. This function is denot
3 min read
Difference between system() and execl() call
A system() call is a way for the programs to interact with the operating system. It is used to execute a command within a process. The system call provides the services of the operating system to the user programs via Application Program Interface (API). It provides an interface between a process an
4 min read
What is the Difference Between Import and Load In Docker?
Docker is a container orchestration platform that is used for containerizing applications. The service offers both free and premium tiers. The software that hosts the containers is known as Docker Engine. Docker import is a Docker command that creates a Docker image by importing material. Docker loa
7 min read
Difference between runtime exception and compile time exception in PHP
The term PHP is an acronym for Hypertext Preprocessor, which is a server-side scripting language designed specifically for web development. It is open-source which means it is free to download and use. It is very simple to learn and use. The files have the extension â.phpâ. It is an interpreted lang
3 min read