1 00:00:00,670 --> 00:00:05,590 Displaying errors to the screen during development is just the first step. 2 00:00:05,590 --> 00:00:09,450 The developer won't always run across every possible error. 3 00:00:09,450 --> 00:00:11,950 And what about errors that happen in production? 4 00:00:11,950 --> 00:00:14,050 That's where logging comes in. 5 00:00:14,050 --> 00:00:15,630 Like other error settings, 6 00:00:15,630 --> 00:00:19,990 ideally your logging should be set in your php.ini file. 7 00:00:19,990 --> 00:00:25,210 However, you can change these settings with the htaccess file or, 8 00:00:25,210 --> 00:00:27,140 in a specific PHP file as well. 9 00:00:28,600 --> 00:00:33,430 Although this is not ideal, you can use the ini_set function 10 00:00:33,430 --> 00:00:38,640 in your PHP file to set the configuration option for logging errors. 11 00:00:38,640 --> 00:00:42,790 It's not an ideal solution, because it will not log any 12 00:00:42,790 --> 00:00:47,030 errors if the script has parse errors and cannot be run at all. 13 00:00:47,030 --> 00:00:48,030 Let's see this in action. 14 00:00:49,390 --> 00:00:53,044 I've turned errors back on in the .htaccess file. 15 00:00:53,044 --> 00:00:54,917 Open logging.php. 16 00:00:54,917 --> 00:01:00,125 At the top of the file we have some log options using the ini_set functions. 17 00:01:00,125 --> 00:01:03,450 log_errors is set to 1. 18 00:01:03,450 --> 00:01:06,873 And error_log is set to php-error.txt. 19 00:01:06,873 --> 00:01:11,960 I'm using .txt for error_log so we can view this file in workspaces. 20 00:01:12,960 --> 00:01:17,950 When we view this script in a browser, we can see our parse error, but 21 00:01:17,950 --> 00:01:22,110 when we go back to work spaces we don't have any error logs. 22 00:01:22,110 --> 00:01:25,070 That's because parse errors won't work. 23 00:01:25,070 --> 00:01:28,970 PHP will never read this line that tells it where error log should be. 24 00:01:30,010 --> 00:01:32,770 If we fix the parse error but leave a syntax error, 25 00:01:32,770 --> 00:01:36,720 you'll see the error logs on the screen and also written to our log. 26 00:01:38,040 --> 00:01:40,346 Let's fix the parse error but leave the syntax error. 27 00:01:44,885 --> 00:01:49,020 Now when we preview it in the browser, we see an error on the screen. 28 00:01:49,020 --> 00:01:50,466 It should also write to our error log. 29 00:01:56,936 --> 00:01:57,750 Great. 30 00:01:57,750 --> 00:01:59,740 The notice and the warning are in our error log. 31 00:02:00,860 --> 00:02:04,150 If we don't have access to the php.ini file, 32 00:02:04,150 --> 00:02:07,970 the next best option is usually the htaccess file. 33 00:02:07,970 --> 00:02:10,510 This is a better option because it will log the errors, 34 00:02:10,510 --> 00:02:13,090 even if there's a parse error in your script. 35 00:02:13,090 --> 00:02:16,625 Let's put our parse error back in, then modify our htaccess file. 36 00:02:32,065 --> 00:02:34,455 We want to turn on log errors. 37 00:02:34,455 --> 00:02:39,354 We use php_value 38 00:02:39,354 --> 00:02:44,590 log_errors 1. 39 00:02:44,590 --> 00:02:46,866 We also need our log file for errors. 40 00:02:49,545 --> 00:02:54,981 php_value error_log 41 00:02:54,981 --> 00:02:59,605 php-error.txt. 42 00:03:07,205 --> 00:03:08,915 Let's rename our file again. 43 00:03:12,915 --> 00:03:18,260 And now when we preview the browser, we see our parse error. 44 00:03:19,580 --> 00:03:24,490 And if we go back to our PHP errors, we now see our parse error is logged as well. 45 00:03:26,090 --> 00:03:30,100 This by no means covers error handling completely. 46 00:03:30,100 --> 00:03:33,190 I trust that it's given you enough to get you started with this 47 00:03:33,190 --> 00:03:35,500 important part of development. 48 00:03:35,500 --> 00:03:39,570 You should now know how to display and log errors. 49 00:03:39,570 --> 00:03:43,240 Proper error handling makes it easier to track down errors and 50 00:03:43,240 --> 00:03:45,410 get your code up and running. 51 00:03:45,410 --> 00:03:49,250 I've included more information on error and exception handling. 52 00:03:49,250 --> 00:03:51,321 As well as debugging in the teacher's notes. 53 00:03:51,321 --> 00:03:53,065 Have fun and keep learning.