passthru()

passthru() — Execute an external program and display raw output.

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

The passthru() function is similar to the exec() function in that it executes a command. This function should be used in place of exec() or system() when the output from the Unix command is binary data which needs to be passed directly back to the browser. A common use for this is to execute something like the pbmplus utilities that can output an image stream directly. By setting the Content-type to image/gif and then calling a pbmplus program to output a gif, you can create PHP scripts that output images directly.

Signature

passthru(string $command, int &$result_code = null): ?false

Parameters

ParameterTypeWhat it takes
$commandstringThe command that will be executed.
&$result_codeintIf the result_code argument is present, the return status of the Unix command will be placed here.

Return value

Returns null on success or false on failure.

Example

The PHP manual ships no example for passthru(); the signature and the parameter table above are the contract.

Changelog

VersionChange
8.0.0If command is empty or contains null bytes, passthru() now throws a ValueError. Previously it emitted an E_WARNING and returned false.

See also

exec(), system(), popen(), escapeshellcmd()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page passthru() (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.