In PHP, pclose() closes a process that was opened with popen(). It waits for the command to finish and then returns its exit status.
You must always close the handle that popen() gave you. If you forget, the command may not finish properly and the output may be lost.
Syntax
Call pclose() with the values you need. Start with this line.
PHP
$status = pclose($handle);
Call
What it does
pclose($handle)
Closes the process and returns its exit code, where 0 normally means success.
pclose(popen('exit 3', 'r'))
Opens a command that exits with 3 and returns 3.
pclose($handle) === 0
Tests whether the command finished without error.
pclose() waits for the command to end, so it can pause your script if the command takes a long time. On Linux the return value is the exit status of the command. A return value of -1 means an error happened while closing.
Library declarations
PHP
pclose(resource $handle): int
Parameters
Parameter
Description
handle
Required. The file pointer returned by popen().
Return Value
Return Value
Description
int
Returns the termination status of the process. A value of 0 usually means success. It returns -1 on error.
PHP Examples
Example 1 — Read a Command and Close It
Run a simple command, read its output and check the exit status.