shell_exec()

shell_exec() — Execute command via shell and return the complete output as a string.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

On Windows, the underlying pipe is opened in text mode which can cause the function to fail for binary output. Consider to use popen() instead for such cases.

Signature

shell_exec(string $command): string|false|null

Parameters

ParameterTypeWhat it takes
$commandstringThe command that will be executed.

Return value

A string containing the output from the executed command, false if the pipe cannot be established or null if an error occurs or the command produces no output. Note: This function can return null both when an error occurs or the program produces no output. It is not possible to detect execution failures using this function. exec() should be used when access to the program exit code is required. This function can return null both when an error occurs or the program produces no output. It is not possible to detect execution failures using this function. exec() should be used when access to the program exit code is required.

Example

A runnable snippet:

$output = shell_exec('ls -lart'); echo "<pre>$output</pre>";

See also

exec(), escapeshellcmd()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page shell_exec() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.