what is the correct way to open file time.txt readable? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

what is the correct way to open file time.txt readable?

10th Aug 2016, 9:23 AM
Lea Rose Tuazon Dabocol
Lea Rose Tuazon Dabocol - avatar
2 Answers
+ 6
oh now i know fopen ("time.txt", "r");
10th Aug 2016, 9:24 AM
Lea Rose Tuazon Dabocol
Lea Rose Tuazon Dabocol - avatar
+ 4
Your answer is correct, but pay attention you must put it in a variable; because fopen()'s return value will use for other functions that are for work on that file, like fclose(). The return value is resource, but if the proccess unsuccessful, it will return false. So it's a good idea to use it in conditions. That's a good work to check file before use it. You can also use file_exists() and is_readable() to protect your code to throw exceptions. This will increase your code security. At last, read these tips: 1. Always close a file with fclose(), even if you are sure that your processes has ended completely and there are no code after it. 2. If you want to write in files, don't use "\n" or "\n\r" forms to go to the next line, because there are differences in OSs. Use global constant "PHP_EOL" that means PHP End Of Line where it puts automatically the correct form of the next line. 3. There is a second way for 2. You can add a "t" after fopen() mode. For example, fopen("item.txt", "rt"). Keep in mind to use "\n" for every new line in "t" mode.
12th Dec 2016, 6:51 PM
$machitgarha
$machitgarha - avatar