What is the purpose of the "rewind" function in PHP and when should it be used?
The "rewind" function in PHP is used to reset the file pointer to the beginning of a file. This can be useful when you want to read the contents of a file multiple times without closing and reopening it. It should be used when you need to iterate over the contents of a file more than once.
$file = fopen("example.txt", "r");
// Read the file contents
while(!feof($file)) {
echo fgets($file) . "<br>";
}
// Rewind the file pointer to the beginning
rewind($file);
// Read the file contents again
while(!feof($file)) {
echo fgets($file) . "<br>";
}
fclose($file);