Visit the Official PHP Website |
PHP ProgrammingBy James N HitzDangerous AssumptionsOur code makes assumptions that:
If the above assumptions were to evaluate to TRUE, our life would just... roll on smoothly but... things are not always what we want and expect them to be. It is very naive to think otherwise. Fortunately, we are in luck because: With these considerations in mind we can alter our code to become a little more smarter: <?php $file2open="/www/home/list.txt"; if(!$listfile=fopen ($file2open, 'r')){ print ("cannot open $file2open for read-only"; } else { print "file: $file2open successfully opened"; if(!fclose($listfile)){ print "cannot close $file2open"; } } ?> If you notice, we are using the bang (!) to test for failure. The line... if(!listfile = fopen($file2open,'r')) ... tests if the file CANNOT be opened by TRYING to open it. Get the logic in that piece of code?! This is sometimes called reverse logic thought it really looks straight to me. Remember in PHP the bang (!) means not true. Fine. In our code we open a file and then immediately close it. What's the point? Let's do something with the data inside the file. |
JamHitz Productions