Php provides web-based functionalities to develop web applications. But it also provides system related scripting and execution features.
exec() function is used to execute an external binary or program from a PHP script or application. In this tutorial, we will look at different use cases and examples of
exec() function like return value, stderr, shell_exec, etc.
exec() Function Syntax
exec()syntax is like below where single parameter is mandatory and other parameters are optional.
exec(string COMMAND, array OUTPUT, int RETURN_VARIABLE);
COMMAND is the command we want to execute with exec() function. The command should be a string value or variable. COMMAND is a mandatory parameter.
OUTPUT is the output of the COMMAND execution. OUTPUT is an array that can hold multiple values or lines. OUTPUT is optional where it can be omitted.
RETURN_VARIABLE is the return value of the given COMMAND. RETURN _VALUE is generally the process status of the command. RETURN_VALUE is an integer and optional to use.
Run External System Binary or Application
We will start with a simple example. We will provide the command we want to run on the local operating system. In this example, we will create a directory named
data. This directory will be created in the current working path. We can also specify the path explicitly like
We can list the created directory with Linux
file command like below. We will also provide the directory name because
exec() function will only show the last line of the output. We will use
echo to print the command output.
echo exec("file data");
We have already looked at printing output but I want to explain more about printing output. exec() function output or return can be printed with echo but the printed part will be only last line. So in a multi-line output, we can not see the output with
echo. If we want to see the whole output we should use the
system() function which will be explained below.
But we can use the
output parameter like below. In this example, we will put command output to the
o. The output parameter is in array type so we will use
print_r to print output.
Assign Return Value into Variable
echo is not a reliable way to get the return value. We can use variables to set return value and use whatever we want. Int this example we will set the process return value to
exec("ls",$o,$v); echo $v;
Return Complete Output as String with shell_exec()
PHP language provides different functions as an alternative to
exec(). We can use the
shell_exec() function which will create a shell process and run given command. In this example, we will look at
ls command and print output. With the
shell_exec() function we can not get the return value of the shell process or command.
PHP shell_exec() Examples
In this part, we will make more examples about the PHP shell_exec() function. We will run different system and Linux commands like
> echo shell_exec('date'); > echo shell_exec('whoami'); > echo shell_exec('ifconfig');
Return Complete Output as String with system()
Another similar function is
system() function displays output directly without using
ls command again.
exec() Function vs shell_exec() Function
PHP also provides the
shell_exec() function which is very similar to the exec() function. The main difference is shell_exec() function accept single parameter which is command and returns the output as string.
//Execude command in the shell with PHP exec() function //put the output to the $output variable exec("uname -a",$output,$return_val); print_r($output); //Execude command in the shell with PHP shell_exec() function //put the output into $out variable $out = shell_exec("uname -a"); echo $out;