0
PHP fopen function
I've learned here that syntax for functions Is function functionName() { *code* } since fopen() is a function, I don't get that fopen function is created by $varname = fopen("filename","action"). And why would you use the fopen or fwrite functions when it's possible to just use the fopen and the letter r or a (read,write only). It's really confusing me Xd
2 Answers
+ 1
So to think of a real life analogy:
fopen
Place the paper next to you.
And also think if you need to write on it (doing homework) or only read (book) it or do both (fill gaps).
fwrite
Use a pencil to actually write on the sheet.
fclose
Put it back again and be happy about your accomplishments :)
+ 1
Function can have a different amount of input variables. In your first example, the function "functionName" has no input variable. So you only need to call functionName()
fopen however needs two variables.
The first one is the file name of the file you want to open. For example the string "myFile.txt"
Your second input is the action, so for example "r" for reading the file. That's why you need to call fopen("myFile.txt", "r")
I think they only mentioned fopen() first to make clear that it is a function but shortened it so the text doesn't blow up.
The difference to fwrite and fopen with writing action is, that with fopen you only open the file and tell what you want to do with it.
To actually store/write something in this opened file, you need to call fwrite("myFile.txt","write this in my file")
If your file "myFile.txt" is not opened or you only set reading for it, then writing won't work.
In the end also remember to call fclose() to tell that you are finished and don't need to touch the file anymore.