rewind() moves the read/write pointer of a file stream to its beginning. This guide explains the function with focused examples and the decisions that matter in real applications.
What Is rewind() in PHP?
Moves the read/write pointer of a file stream to its beginning.
A typical call looks like this:
<?php
rewind($stream);
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 rewind() directly to demonstrate its basic purpose.
<?php
$stream = fopen("php://temp", "rw"); fwrite($stream, "Hello"); rewind($stream); echo stream_get_contents($stream); fclose($stream);
The returned value illustrates moves the read/write pointer of a file stream to its beginning. Only seekable streams support rewinding; check the returned boolean.
Example 2: A Different Input or Option
The next example changes the input or option, showing another supported use of rewind().
<?php
$stream = fopen("php://temp", "rw"); fwrite($stream, "One Two"); rewind($stream); echo fgets($stream); fclose($stream);
The returned value illustrates moves the read/write pointer of a file stream to its beginning. Only seekable streams support rewinding; check the returned boolean.
Practical Example: Applying the Function
In application code, rewind() belongs in a controlled workflow with input validation, a checked result and cleanup where needed.
<?php
$stream = fopen("php://temp", "rw"); fwrite($stream, "report-data"); if (rewind($stream)) echo stream_get_contents($stream); fclose($stream);
The returned value illustrates moves the read/write pointer of a file stream to its beginning. Only seekable streams support rewinding; check the returned boolean.
Important Behavior and Edge Cases
- Main behavior: Moves the read/write pointer of a file stream to its beginning.
- Important limitation: Only seekable streams support rewinding; check the returned boolean.
- Requirements: This operation is available in standard PHP; validate inputs and handle failures where relevant.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
rewind() | Moves the read/write pointer of a file stream to its beginning. |
fseek | Moves a stream pointer to an explicitly chosen offset; rewind() goes to the beginning. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the specific limits or preconditions of rewind(). | Only seekable streams support rewinding; check the returned boolean. |
| Assuming every environment produces the same result. | This operation is available in standard PHP; validate inputs and handle failures where relevant. |
Key Takeaways
- Moves the read/write pointer of a file stream to its beginning.
- Only seekable streams support rewinding; check the returned boolean.
- This operation is available in standard PHP; validate inputs and handle failures where relevant.
Use rewind() when its behavior matches your task, and verify the relevant return value before taking further action. The output depends on the local configuration when it uses resources, files or an interactive terminal.