Use flush() when you need to asks PHP to send any buffered response data toward the client in a PHP application. The examples below show the actual operation and its limitations.
What Is flush() in PHP?
Asks PHP to send any buffered response data toward the client. The exact signature supported by your installation depends on its PHP version and enabled extensions.
Its syntax is:
flush(): void
Parameters and return value: The parameter types, optional arguments, references, and return type are shown in the signature above. Optional parameters are marked with an ellipsis in this reference signature.
Example 1: Basic Usage
<?php
echo "First part"; flush();
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Example 2: Different Input or Behavior
<?php
echo "One"; flush(); echo "Two";
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Practical Example: Applying the Function
<?php
$chunks = ["Loading", "Working", "Ready"]; foreach ($chunks as $chunk) { echo $chunk . "
"; flush(); }
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Important Behavior and Edge Cases
flush() cannot force delivery through web servers, proxies, or output buffers.
Comparison With Related Functions
| Approach | Difference |
|---|---|
flush() | Asks PHP to send any buffered response data toward the client. |
| Related operation | ob_flush() flushes a PHP output buffer; flush() requests transmission toward the client. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring the function-specific limitation | flush() cannot force delivery through web servers, proxies, or output buffers. |
| Confusing this function with a related operation | ob_flush() flushes a PHP output buffer; flush() requests transmission toward the client. |
Key Takeaways
- Asks PHP to send any buffered response data toward the client.
- flush() cannot force delivery through web servers, proxies, or output buffers.
- ob_flush() flushes a PHP output buffer; flush() requests transmission toward the client.
Use flush() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.