At first, RTFM.
Imagine the file as array of bytes (I hope, you know what array word means). Imagine, that you have number "i" and trying to get element with index i form array.
$c = $ar[$i];
And this "i" is file position 😉 You can adjust it with fseek function. Now about open modes:
"r" (read) - you can only read from file. initial position - 0.
"r+" (read write) - you can read and write. initial position - 0. so, if you want to add something to the end, you should:
fseek($f, 0, SEEK_END);
dowrite($f);
"w" (write) - creates new file for writing only (if file exists, it wipes all content)
"w+" (write read) - the same as previous, but it allows you to read data, you wrote to file (never have used this one)
"a" (append) - write only, if file exists, it keep content and poistions to the end, otherwise it creates new one.
"a+" (append read) - the same as previous, but allows you read.
So, here is simple algorythm, which mode to choose:
1. If you only need to read, use "r"
2. If you need to read an, probably, change some data, use "r+"
3. If you need to create new file, use "w"
4. If you need to append data to existing file or create new one, use "a"
5. If you need the same as in "4" plus reading, use "a+" or "w+" - (they differ only with starting position)