Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php

What is the rewind() function in PHP?

Behzad Ahmad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The rewind() function in PHP is a built-in function used to rewind the position of the file pointer. This means that the position of the file pointer is set to the start of the file.

Syntax

rewind(resource $stream): bool

Parameters

stream: A file stream that points to an already opened file.

If a file is opened in “append” mode, i.e., "a" or "a+", the position of the pointer continues from where the file was last written, regardless of the position set.

Return value

rewind() returns true if it is successfully executed. Otherwise, in case of any error, it returns false.

Code

The following code shows how the rewind() function works.

main.php
q1.txt
<?php
$temp = fopen("q1.txt","r+");
// display the contents of the file
echo readfile("q1.txt");
//reset the file pointer
rewind($temp);
//add content at the start of the file, overwriting it
fwrite($temp, "\nHello world! ");
//read the file again
echo readfile("q1.txt");
fclose($temp);
?>

RELATED TAGS

php

CONTRIBUTOR

Behzad Ahmad
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring