PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY If (Unable to start debugging on the web server)
PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY If the file_get_contents() function can t open the file, it returns false. Often, you can test for false by using the negative operator like this: if (!$contents) { The reason I haven t used that shortcut here is because the external file might be empty, or you might want it to store a number. As explained in The truth according to PHP in Chapter 3, an empty string and 0 also equate to false. So, in this case, I ve used the identical operator (three equal signs), which ensures that both the value and the data type are the same. 6. Test the page in a browser, and it should work as before. Change the first line like this so that it loads filetest02.txt: $contents = file_get_contents(’C:/private/filetest02.txt’); The new text file contains the number 0, which should display correctly when you test file_get_contents.php. Delete the number in filetest02.txt, and reload file_get_contents.php. You should get a blank screen, but no error message. This indicates that the file was loaded successfully, but doesn t contain anything. 7. Change the first line in file_get_contents.php so that it attempts to load a nonexistent file, such as filetest0.txt. When you load the page, you should see an ugly error message reporting that file_get_contents() failed to open stream in other words, it couldn t open the file. 8. This is an ideal place to use the error control operator (see Chapter 4). Insert an @ mark immediately in front of the call to file_get_contents() like this: $contents = @ file_get_contents(’C:/private/filetest0.txt’); 9. Test file_get_contents.php in a browser. You should now see only the following custom error message: Always add the error control operator only after testing the rest of a script. When devel- oping, error messages are your friends. You need to see them to understand why some- thing isn t working the way you expect. Text files can be used as a flat-file database where each record is stored on a separate line, with a tab, comma, or other delimiter between each field (see http://en.wikipedia.org/ wiki/Flat_file_database). When handling this sort of file, it s more convenient to store each line individually in an array ready for processing with a loop. The PHP file() function builds the array automatically.
From our experience, we can recommend PHP5 Web Hosting services, if you need affordable webhost to host and run your web application.