proc_close() closes a process opened with proc_open() and returns its exit status. This guide explains the function with focused examples and the decisions that matter in real applications.
What Is proc_close() in PHP?
Closes a process opened with proc_open() and returns its exit status.
A typical call looks like this:
<?php
proc_close($process);
The input arguments shown in the call identify the relevant connection, resource, value, path or configuration for the operation. Pay attention to argument order and to the return value demonstrated below.
Example 1: Basic Usage
This first example uses proc_close() directly to demonstrate its basic purpose.
<?php
$process = proc_open(["php", "-r", "echo 1;"], [1 => ["pipe", "w"]], $pipes); fclose($pipes[1]); echo proc_close($process);
The returned value illustrates closes a process opened with proc_open() and returns its exit status. Close opened pipes before waiting to avoid deadlocks.
Example 2: A Different Input or Option
The next example changes the input or option, showing another supported use of proc_close().
<?php
$process = proc_open(["php", "-r", "exit(3);"], [], $pipes); echo proc_close($process);
The returned value illustrates closes a process opened with proc_open() and returns its exit status. Close opened pipes before waiting to avoid deadlocks.
Practical Example: Applying the Function
In application code, proc_close() belongs in a controlled workflow with input validation, a checked result and cleanup where needed.
<?php
$process = proc_open(["php", "-r", "echo 123;"], [1 => ["pipe", "w"]], $pipes); echo stream_get_contents($pipes[1]); fclose($pipes[1]); echo proc_close($process);
The returned value illustrates closes a process opened with proc_open() and returns its exit status. Close opened pipes before waiting to avoid deadlocks.
Important Behavior and Edge Cases
- Main behavior: Closes a process opened with proc_open() and returns its exit status.
- Important limitation: Close opened pipes before waiting to avoid deadlocks.
- Requirements: The behavior and privileges of child processes vary across systems.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
proc_close() | Closes a process opened with proc_open() and returns its exit status. |
proc_terminate | Sends a termination signal to a child process started through proc_open(). |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the specific limits or preconditions of proc_close(). | Close opened pipes before waiting to avoid deadlocks. |
| Assuming every environment produces the same result. | The behavior and privileges of child processes vary across systems. |
Key Takeaways
- Closes a process opened with proc_open() and returns its exit status.
- Close opened pipes before waiting to avoid deadlocks.
- The behavior and privileges of child processes vary across systems.
Use proc_close() when its behavior matches your task, and verify the relevant return value before taking further action. Run examples that affect processes, users, privileges or the filesystem only in a controlled development environment.