1 00:00:00,870 --> 00:00:05,270 Turning on errors within an individual PHP file can be helpful and 2 00:00:05,270 --> 00:00:09,080 quick, especially if you don't have access to the server, or 3 00:00:09,080 --> 00:00:12,540 you're just trying to track down an error on one page. 4 00:00:12,540 --> 00:00:17,630 This also allows you to override the settings in the PHP.INI file. 5 00:00:17,630 --> 00:00:23,237 We can use this to turn on, off, or change the level of errors to display. 6 00:00:23,237 --> 00:00:27,121 The problem with using only your own PHP file is, 7 00:00:27,121 --> 00:00:32,770 parse errors will stop PHP from processing your code at all. 8 00:00:32,770 --> 00:00:37,090 This means, PHP will never even see those settings. 9 00:00:37,090 --> 00:00:41,240 These shortcomings aside, sometimes this is your best option at the time, and 10 00:00:41,240 --> 00:00:44,270 you need to change settings momentarily. 11 00:00:44,270 --> 00:00:46,540 Or for a particular file. 12 00:00:46,540 --> 00:00:48,510 Let's explore these options in Workspaces. 13 00:00:50,030 --> 00:00:53,670 We're going to go back and turn off errors with our .htaccess file. 14 00:01:30,545 --> 00:01:33,777 Now we can go back to our browser and preview errors one. 15 00:01:37,203 --> 00:01:39,340 We see a blank white page. 16 00:01:39,340 --> 00:01:43,330 There aren't any parse errors because we're not seeing the error page. 17 00:01:43,330 --> 00:01:46,470 So we can go in and turn on errors just for this page. 18 00:01:48,350 --> 00:01:50,690 To tell the server we want to display errors, 19 00:01:50,690 --> 00:01:56,650 we add two lines to the top of our PHP file directly below the opening PHP tag. 20 00:01:56,650 --> 00:01:59,630 I've already added these lines, but they're commented out. 21 00:02:00,820 --> 00:02:03,580 We'll remove the double slashes before the two lines. 22 00:02:04,700 --> 00:02:06,530 The first is error reporting. 23 00:02:08,250 --> 00:02:10,290 This sets error reporting to all. 24 00:02:11,340 --> 00:02:15,600 The next one is the ini_set display_errors. 25 00:02:17,850 --> 00:02:22,712 One means true, display errors on. 26 00:02:22,712 --> 00:02:26,590 I've also included the setting for html_errors just in case. 27 00:02:28,560 --> 00:02:31,344 After saving the file, let's refresh the browser. 28 00:02:33,889 --> 00:02:35,010 Awesome. 29 00:02:35,010 --> 00:02:36,745 We have a notice and a warning. 30 00:02:36,745 --> 00:02:41,360 Notice: Undefined variable: errors, 31 00:02:41,360 --> 00:02:44,660 in our errors1 on line 12. 32 00:02:44,660 --> 00:02:49,460 And our warning tells us that there's an invalid argument supplied for 33 00:02:49,460 --> 00:02:51,310 foreach on line 12 as well. 34 00:02:53,040 --> 00:02:56,750 These are both telling us there's an error on line 12, so let's take a look. 35 00:02:58,790 --> 00:03:03,275 Sure enough on line 12 we see that we have tried to loop through an array named 36 00:03:03,275 --> 00:03:04,540 errors. 37 00:03:04,540 --> 00:03:08,105 But the actual array we want is named error_levels. 38 00:03:09,310 --> 00:03:11,845 Let's change this name. 39 00:03:11,845 --> 00:03:14,620 And then we can save the page and refresh the browser. 40 00:03:17,080 --> 00:03:19,730 Our errors are gone and our page is working. 41 00:03:19,730 --> 00:03:20,230 Great job! 42 00:03:22,200 --> 00:03:27,740 You can also suppress errors for a particular line using the @ symbol. 43 00:03:27,740 --> 00:03:33,150 This can be useful if you actually want to allow things through without a warning or 44 00:03:33,150 --> 00:03:36,180 if you want to hide a warning for short time. 45 00:03:36,180 --> 00:03:38,610 Let's go back into work spaces and see this in action. 46 00:03:40,270 --> 00:03:42,288 Let's take a look at errors3. 47 00:03:44,773 --> 00:03:47,710 We have a file that has a bunch of errors. 48 00:03:47,710 --> 00:03:50,710 Let's see what happens when we suppress those errors. 49 00:03:50,710 --> 00:03:53,320 The first one we see is a warning, and 50 00:03:53,320 --> 00:03:58,700 a fatal error that have to do with not being able to open a required file. 51 00:03:59,890 --> 00:04:03,015 The call to this required file is on line ten. 52 00:04:07,847 --> 00:04:12,630 If we suppress our call to required field we can suppress our errors. 53 00:04:14,700 --> 00:04:19,840 There are no warnings, but the code will stop because the file is required. 54 00:04:19,840 --> 00:04:21,795 So let's comment out that line for now. 55 00:04:28,472 --> 00:04:33,490 Now we see a few more warnings because it failed to open files for include. 56 00:04:34,620 --> 00:04:38,113 These files are found on lines 13 and 16. 57 00:04:40,292 --> 00:04:46,724 Let's suppress line 13 and also the file on line 16. 58 00:04:48,367 --> 00:04:50,670 Include files are not required. 59 00:04:50,670 --> 00:04:53,707 So if we suppress the warnings, we can continue on. 60 00:04:56,257 --> 00:05:00,068 We see that we've stopped at line 16 and displayed the message, 61 00:05:00,068 --> 00:05:01,300 failed to open file. 62 00:05:02,420 --> 00:05:05,390 That's because we told our script to die or 63 00:05:05,390 --> 00:05:08,750 stop and display a message to the screen. 64 00:05:08,750 --> 00:05:10,800 You could argue that this is a valid use for 65 00:05:10,800 --> 00:05:15,430 line suppression because we are actually doing something with the error. 66 00:05:15,430 --> 00:05:17,940 Let's comment out this line and see what else we have. 67 00:05:24,229 --> 00:05:26,040 Finally we have three more errors. 68 00:05:27,060 --> 00:05:30,720 The first is a warning telling us that we're missing argument 1 for 69 00:05:30,720 --> 00:05:37,750 error function called on line 25 and define on line 18. 70 00:05:37,750 --> 00:05:40,110 The second and third are both notices, 71 00:05:40,110 --> 00:05:42,990 and they tell us that we have an undefined variable. 72 00:05:44,310 --> 00:05:50,075 The variable bad is at line 19 and the variable error is on line 21. 73 00:05:52,027 --> 00:05:57,690 We can see on line 19 that bad is actually an undefined variable. 74 00:05:57,690 --> 00:06:02,780 On line 21, the error variable is the argument we're supposed to pass. 75 00:06:04,150 --> 00:06:07,278 Let's suppress our function call on line 25. 76 00:06:12,269 --> 00:06:15,635 If we suppress our function call, all three errors go away, 77 00:06:15,635 --> 00:06:20,330 even though our variable bad has nothing to do with the missing argument. 78 00:06:20,330 --> 00:06:23,530 And it's probably something that needs to be addressed. 79 00:06:23,530 --> 00:06:27,940 This should give you a sample of how and when you might suppress errors and 80 00:06:27,940 --> 00:06:30,050 some of the issues that they can cause. 81 00:06:31,820 --> 00:06:35,380 Anytime you are suppressing errors in any form, 82 00:06:35,380 --> 00:06:37,530 make sure this is actually what you want. 83 00:06:38,610 --> 00:06:41,070 Warnings are there for a reason. 84 00:06:41,070 --> 00:06:45,650 Line suppressions can make things especially difficult to diagnose so 85 00:06:45,650 --> 00:06:48,900 use them sparingly and only when necessary. 86 00:06:50,560 --> 00:06:55,300 Often exceptions are a much better way to handle these types of situations. 87 00:06:55,300 --> 00:06:58,280 Don't forget to check the teacher's notes for more information.