Now that we've talked about terminating our PHP code and using the Die Function for troubleshooting and debugging, let's talk a little bit more about how we can write an error log for further debugging or even administering our web application. You can do this in PHP with the Error Log Function. This is a built-in function of php and is documented on their website, PHP.Net. I'm going to be showing this example in the 0308.php project file. Here we have a For Loop that's a little bit familiar to you because we just covered this in the previous lesson. And I'm going to change the Die Function to actually be the Error Log Function. I'm going to type in error log and put my parenthesis where I'm going to pass the parameters that I need. And in the parenthesis I'm going to pass three parameters. The first one is going to be the text message, it's going to be string. The second one is going to be an integer of three. You can use different integers which specify how the error log is to operate and you could see these documented at the PHP.net website. And the third parameter is going to be the location of my log file. Now when you specify your location you need to remember that the web server needs write privileges to that location. So pick a safe one that the web server can write to. On Max OS X or a Unix machine the web server can write to the Temp File which is at the root level of the hard drive. So I'm going to go ahead and in my text I'm going to put the path to the Temp Drive and then the name of my file which will be PHP Log File.TXT so text file. And I'm going to use double quotes just out of habit. The three means that we are going to append to the existing text file or create one if it's not there and that's the number of the integer you'll most likely use. Our text message can be anything. I'm just going to put text message. So let's open up my temp directory and go ahead and run this code 0308 and create my log file. Here you can see the PHP log file was created. When I open that up in my text viewer you can see that for every time I was greater than 11, it went ahead and iterated or it went ahead and appended to the log file, my message. One of the things you can see here is it did not append the line return so I'm going to need to add that. And also for debugging purposes you may want to specify something such as a timestamp or a date and timestamp so you know that when the log file was touched or written to. To do that I'm going to go ahead and just throw away this log file and then I'm going to modify my message to include maybe the current time. This would be a Unix timestamp. I could change it to a date by formatting it with the Date Function but for right now we'll just put the timestamp. And then after my message well I'll put a little separator here between the timestamp and the message. And then afterwards the backslash R which is a line return. Now this backslash R only works when you're using double quotes so make sure you have double quotes. And let's go ahead and refresh this page, create a new log file. Let's update this window and we get the log file. Now you can see that I have a timestamp and I have the text message. Now let's go ahead and say that if the condition is true we want to exit the script as well. So we're going to use our curly braces because we have more than one statement after the condition is true or met. And let's go ahead and update that log file and refresh this a few times. And now we'll open that log file up and here you can see the difference in the timestamp. Before you didn't because it ran so fast it was all within a second so the timestamp didn't change. So you can also concatenate dynamic data. So the text message was written when I was equal to and now we can go ahead and keep appending to that log file. Open it back up and now you can see how you can create a log file for debugging and administrative purposes.
| Course: | FileMaker and PHP Foundations |
| Author: | Lance Hallberg |
| SKU: | 34393 |
| ISBN: | 978-1-61866-077-0 |
| Release Date: | 2012-12-07 |
| Duration: | 7.5 hrs / 97 lessons |
| Work Files: |
Yes |
| Captions: | No |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |