Why there is need to use only r+ or r?

<?php ## process Model-reader.
$file = "file.txt";

// First create an empty file IF IT does NOT.
// If the file exists, it won't disturb it.
fclose(fopen($file, "a+b"));

// Block the file.
$f = fopen($file, "r+b") or die("cannot open file!");
flock($f, LOCK_SH); // wait until writer

 // . . .
 // At this point we can be sure that the file 
 // no one writes.
 // . . . 

// All done. Remove the lock.
fclose($f);
?>

the author encourages the use here only fopen($file, "r+b") or fopen($file, "rb"), but when the mode a+, fseek() works fine for reading, but in this case afterflock($f, LOCK_SH); and to fclose($f); file is read-only, then why only ror r+?
April 3rd 20 at 18:55
1 answer
April 3rd 20 at 18:57
Solution
Modes r/r+ set the pointer at the beginning of the file, and allow you to write in any positionby sliding the pointer using fseek.

Modes are a/a+ sets the pointer at the end of the file, and allow you to write only at the end of the file. fseek only affects the reading.

https://www.php.net/manual/en/function.fopen.php
In this case, after lock is used only reading - jackie.Schum commented on April 3rd 20 at 19:00
@jackie.Schum, then it does not matter. If you know you have to read from the beginning - the "r" at the end - "a". - norberto_Lebsa commented on April 3rd 20 at 19:03

Find more questions by tags PHP