exec

FunctionParams and return types changed in PHP 8.0

Execute an external program.

PHP 5
PHP 7
PHP 8.0
Improved
PHP 8.1
PHP 8.2-8.3
PHP 8.4
PHP 8.5
PHP 8.6

exec Function synopsis

exec(
    string $command,
    &$output = null,
    &$result_code = null
  ): string|false

Parameters

$command

Typestring

The command that will be executed.

$output

Passed by reference, OptionalDefault valuenull

If the $output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included in this array. Note that if the array already contains some elements, exec will append to the end of the array. If you do not want the function to append elements, call unset on the array before passing it to exec.

$result_code

Passed by reference, OptionalDefault valuenull

If the $result_code argument is present along with the $output argument, then the return status of the executed command will be written to this variable.

Note that the parameters $output and $result_code are passed by reference, and contain additional/updated context data that may be useful.

Return value

Typestring|false

The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru function.

Returns false on failure.

To get the output of the executed command, be sure to set and use the $output parameter.

Changes to the exec Function

PHP 8.0

  • Return type added: string|false
  • Parameter type added for parameter #1 ($command): string
  • Parameter default value added for position #2 ($output): null
  • Parameter name of parameter #3 changed: $return_value to $result_code
  • Parameter default value added for position #3 ($result_code): null
  exec(
-     $command,
+     string $command,
-     &$output,
+     &$output = null,
-     &$return_value
+     &$result_code = null
-   )
+   ): string|false

exec Function Availability

PHP VersionAvailability
PHP 8.6Upcoming Release Yes
PHP 8.5Supported (Latest) Yes
PHP 8.4Supported Yes
PHP 8.3Security-Fixes Only Yes
PHP 8.2Security-Fixes Only Yes
PHP 8.1Unsupported Yes
PHP 8.0Unsupported Yes
PHP 7.4Unsupported Yes
PHP 7.3Unsupported Yes
PHP 7.2Unsupported Yes
PHP 7.1Unsupported Yes
PHP 7.0Unsupported Yes
PHP 5.6Unsupported Yes
PHP 5.5Unsupported Yes
PHP 5.4Unsupported Yes
PHP 5.3Unsupported Yes