What is the pclose() function in PHP?

pclose() closes a pipe opened by the popen() function. The file pointer initiated by the popen() function must be closed with pclose().

Syntax

pclose(resource $handle): int

Parameters

  • handle: The file pointer returned by the call to the popen() function.

Return value

pclose() returns the termination status of the process that was run. 1-1 is returned in case of an error.

Code

The following code demonstrates the use of pclose(). Here, the termination status returned from the call to pclose() is stored in $exit_status and printed out to the standard output.

<?php
$handle = popen('/bin/ls', 'r');
$read = fread($handle, 2048);
$exit_status = pclose($handle);
print_r("Exit Status: $exit_status\n");
?>
Copyright ©2024 Educative, Inc. All rights reserved