popen()

popen() — Opens process file pointer.

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

Opens a pipe to a process executed by forking the command given by command.

Signature

popen(string $command, string $mode): resource|false

Parameters

ParameterTypeWhat it takes
$commandstringThe command
$modestringThe mode. Either 'r' for reading, or 'w' for writing.

Return value

Returns a file pointer identical to that returned by fopen(), except that it is unidirectional (may only be used for reading or writing) and must be closed with pclose(). This pointer may be used with fgets(), fgetss(), and fwrite(). When the mode is 'r', the returned file pointer equals to the STDOUT of the command, when the mode is 'w', the returned file pointer equals to the STDIN of the command. If an error occurs, returns false.

Example

A runnable snippet:

$handle = popen("/bin/ls", "r");

See also

pclose(), fopen(), proc_open()

All PHP 8.5 built-in functions

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