Flushes PHP buffered output for a writable file stream.
What Is fflush() in PHP?
Flushes PHP buffered output for a writable file stream.
Its syntax is:
fflush(resource $stream): bool
Parameters:
$stream— Accepted as resource; required argument.
Return type: bool. fflush() does not guarantee durable disk persistence; fsync() requests stronger synchronization.
Availability: Requires a valid writable stream.
Example 1: Basic Usage
<?php
$stream = tmpfile(); fwrite($stream, "abc"); var_dump(fflush($stream)); fclose($stream);
Output:
bool(true)
Fflush() does not guarantee durable disk persistence; fsync() requests stronger synchronization.
Example 2: A Different Input
<?php
$stream = tmpfile(); fwrite($stream, "report"); echo fflush($stream) ? "Flushed" : "Error"; fclose($stream);
Output:
Flushed
Fflush() does not guarantee durable disk persistence; fsync() requests stronger synchronization.
Practical Example: Applying It in a Program
<?php
$stream = tmpfile(); fwrite($stream, "checkpoint"); fflush($stream); rewind($stream); echo stream_get_contents($stream); fclose($stream);
Output:
checkpoint
Fflush() does not guarantee durable disk persistence; fsync() requests stronger synchronization.
Important Behavior and Edge Cases
- Key behavior: fflush() does not guarantee durable disk persistence; fsync() requests stronger synchronization.
- Environment: Requires a valid writable stream.
Comparison With Related Functions
| Function | Purpose |
|---|---|
fflush() | Flushes PHP buffered output for a writable file stream. |
fsync() | fsync() asks to synchronize file data and metadata; fdatasync() concentrates on data and required metadata. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this behavior: fflush() does not guarantee durable disk persistence; fsync() requests stronger synchronization | fflush() does not guarantee durable disk persistence; fsync() requests stronger synchronization. |
Key Takeaways
- Flushes PHP buffered output for a writable file stream.
- fflush() does not guarantee durable disk persistence; fsync() requests stronger synchronization.
- Requires a valid writable stream.
Summary: fflush() does not guarantee durable disk persistence; fsync() requests stronger synchronization.