PHP | pclose( ) Function
The pclose() closes a pipe opened by the popen() function. The file pointer initiated by the popen() function must be closed with pclose().
The pipe specified by popen() function is sent as a parameter to the pclose() function and it returns the termination status of the process that was run, or -1 in case of an error.
Syntax:
pclose(pipe)
Parameters Used:
The pclose() function in PHP accepts only one parameter.
- pipe : It is a mandatory parameter which specifies the pipe opened by the popen() function.
Return Value:
It returns the termination status of the process that was run, or -1 in case of an error.
Errors And Exceptions:
- To obtain the real exit status code the pcntl_wexitstatus() function should be used.
- pclose() returns 0 on every platform in case popen() could not execute the specified command.
Examples:
Input : $my_file = popen("/bin/ls", "r");
pclose($my_file);
Output : 1
Input : $my_file = popen('/executable/gfg.exe', 'r');
echo "'my_file'; " . get_class($my_file) . "\n";
$file_read = fread($my_file, 4192);
echo $file_read;
pclose($my_file);
Output : 1
Below programs illustrate the pclose() function.
Program 1
<?php // opening a pipe $my_file = popen("/bin/ls", "r"); // closing the my_file pclose($my_file); ?> |
Output:
1
Program 2
<?php // opening a pipe $my_file = popen('/executable/gfg.exe', 'r'); // returning name of class of an object using get_class() echo "'$my_file'; " . get_class($my_file) . "\n"; // reading file using fread() $filereader = fread($my_file, 4192); echo $filereader; // closing the pipe pclose($my_file); ?> |
Output:
1
Reference:
http://php.net/manual/en/function.pclose.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to get the function name from within that function using JavaScript ?
- D3.js | d3.max() function
- PHP | exp() Function
- PHP | each() Function
- PHP | Ds\Map xor() Function
- PHP | Ds\Map put() Function
- PHP | pow( ) Function
- PHP | Ds\Set xor() Function
- PHP | key() Function
- CSS | rgb() Function
- PHP | min( ) Function
- PHP | pos() Function
- PHP | end() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



