Open In App

PHP | shell_exec() vs exec() Function

Last Updated : 31 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

shell_exec() Function

The shell_exec() function is an inbuilt function in PHP which 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 return NULL and the values are not reliable for error checking.

Syntax:

string shell_exec( $cmd )

Parameters: This function accepts single parameter $cmd which is used to hold the command that will be executed.

Return Value: This function returns the executed command or NULL if an error occurred.

Note: This function is disabled when PHP is running in safe mode.

Example:




<?php
  
// Use ls command to shell_exec
// function
$output = shell_exec('ls');
  
// Display the list of all file
// and directory
echo "<pre>$output</pre>";
?>


Output:

gfg.php
index.html
geeks.php

exec() Function

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 )

Parameters: This function accepts three parameters as mentioned above and described below:

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

Return Value: This function returns the executed command, be sure to set and use the output parameter.

Example:




<?php
// (on a system with the "iamexecfunction" executable in the path)
echo exec('iamexecfunction');
?>


Output:

geeks.php

References:

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


Similar Reads

Explain the Difference Between shell_exec() and exec() Functions
In this article, we will learn about the shell_exec() &amp; 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 fun
4 min read
JavaScript RegExp exec() Method
The exec() Method in JavaScript is used to test for the match in a string. If there is a match this method returns the first match else it returns NULL. Syntax: RegExpObject.exec(str) Parameters: str: It is the string to be searched. This is a required field. Return value: This method returns an array that contains the matched text if the match is
2 min read
How does Query.prototype.exec() work in Mongoose?
The Query.prototype.exec() function is used to execute the query. It can handle promises and executes the query easily. The callback can be passed as an optional parameter to handle errors and results. Syntax: Query.prototype.exec() Parameters: This function has two optional parameters, one is callback function and another is operation of string or
2 min read
Mongoose Aggregate.prototype.exec() API
The Aggregate API.prototype.exec() method of the Mongoose API is used to perform aggregation tasks. It allows us to execute the aggregate pipeline on the current Model. It takes a callback function as a parameter and returns a promise. Syntax: aggregate.exec( callback ) Parameters: This method accepts a single parameter as discussed below: callback
3 min read
Mongoose Query.prototype.exec() API
The Mongoose Query API.prototype.exec() method of the Mongoose API is used on the Query objects. It allows us to execute the query operation to get the resulted data. Using this method we can also provide the query operation as a parameter to the method. Let us understand the exec() method using an example. Syntax: query.exec( operation, callback )
3 min read
How to get the function name inside a function in PHP ?
To get the function name inside the PHP function we need to use Magic constants(__FUNCTION__). Magic constants: Magic constants are the predefined constants in PHP which is used on the basis of their use. These constants are starts and end with a double underscore (__). These constants are created by various extensions. Syntax: $string = __FUNCTION
1 min read
How the User-Defined Function differs from Built-in Function in PHP ?
In PHP, functions are blocks of reusable code that perform specific tasks. They enhance code readability, modularity, and maintainability by encapsulating logic into named units. PHP functions can be categorized into two main types: user-defined functions and built-in functions. User-Defined FunctionsUser-defined functions are functions created by
2 min read
PHP 5 vs PHP 7
PHP is a server side scripting language designed for web development by Rasmus Lerdorf in 1994. Since its launch in 1994 PHP has become an industry standard supporting almost 80% of the websites ( 79.8% to be precise) with its closest competitor being ASP.Net at 19.8% and others like Ruby, Java trailing far behind. The PHP development team released
4 min read
PHP | Get PHP configuration information using phpinfo()
PHP provides us with a built-in function phpinfo() which gives us the details about the PHP version and PHP configuration of PHP installed in our system. To know about the Configurations and PHP version which is installed in your computer, a simple PHP script can be used. The script consists of a PHP function called "phpinfo()" which outputs inform
2 min read
PHP | php.ini File Configuration
At the time of PHP installation, php.ini is a special file provided as a default configuration file. It's very essential configuration file which controls, what a user can or cannot do with the website. Each time PHP is initialized, the php.ini file is read by the system. Sometimes you need to change the behavior of PHP at runtime, then this config
5 min read