PHP pclose() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

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);
CallWhat 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) === 0Tests 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

ParameterDescription
handleRequired. The file pointer returned by popen().

Return Value

Return ValueDescription
intReturns 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.

PHP
<?php
$handle = popen('echo Hello', 'r');

echo fgets($handle);

$status = pclose($handle);
echo "Exit status: $status", PHP_EOL;

How It Works

  1. popen() starts the command echo Hello and lets PHP read its output.
  2. fgets() reads the line Hello that the command prints.
  3. pclose() closes the process and returns its exit status.
  4. The status 0 means that the command ended without problems.

Example 2 — A Command That Fails

A non-zero status tells you that the command did not end normally.

PHP
<?php
$handle = popen('exit 3', 'r');

$status = pclose($handle);

echo "Exit status: $status", PHP_EOL;

How It Works

  1. The shell command exit 3 stops at once and reports the number 3.
  2. pclose() waits for the command and collects that number.
  3. The status is stored in $status and printed.
  4. Any value other than 0 normally means an error or a special result.

Example 3 — Read Several Lines Before Closing

Loop until the end of the output, then close the process and report the result.

PHP
<?php
$handle = popen('echo one && echo two', 'r');

while (!feof($handle)) {
    $line = fgets($handle);
    if ($line !== false) {
        echo "Line: ", trim($line), PHP_EOL;
    }
}

echo "Closed with status ", pclose($handle), PHP_EOL;

How It Works

  1. popen() runs two echo commands, one after the other.
  2. feof() tells the loop when no more output is left.
  3. fgets() returns false at the end, so the if check prevents an empty line from being printed.
  4. pclose() closes the process and returns 0 for success.

Frequently Asked Questions

It closes a process that was opened with popen() and returns the exit status of the command.
No. A handle from popen() should be closed with pclose(), which also waits for the command and gives you the exit status.
The termination status of the command. 0 normally means success, any other number means an error, and -1 means pclose() itself failed.
pclose() waits for the command to finish. If the command runs for a long time, your script waits as well.
Only if you control the command. Never put raw user input in it. Use escapeshellarg() on any value you add.

Next: popen()

popen() runs a shell command and opens a pipe to it. You can then read the output of the c.

popen() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful