Your Own PHP File6:59 with Alena Holligan
Turning on error within an individual PHP file can be helpful and quick, especially if you don’t have access to the server, or are just trying to track down an error on one page. This also allows you to override the setting in the php.ini or .htaccess file. You can also suppress errors for a particular line using the @ symbol.
PHP Error Settings
error_reporting(E_ALL); ini_set("display_errors", 1); ini_set("html_errors", 1);
WARNING! Be careful that your error handling does not get pushed to production.
This does not work for Parse Errors which stop PHP from processing your code at all. PHP will never even read those error setting lines of code.
Using a php file to debug sytax errors
When you don't have access to your php.ini file, it can be difficult to debug syntax errors since they cause fatal startup errors. You can use the following method to bypass this limitation:
Create a file "syntax.debug.php" with the following code (replace "syntax.php" with the file you want to test):
<?php error_reporting(E_ALL); ini_set('display_errors','On'); include('syntax.php');
The 5 line file is guaranteed to be free of errors, allowing PHP to execute the directives within it before including the file which previously caused fatal startup errors. Now those fatal startup errors become run time fatal errors.
Turning on errors within an individual PHP file can be helpful and 0:00 quick, especially if you don't have access to the server, or 0:05 you're just trying to track down an error on one page. 0:09 This also allows you to override the settings in the PHP.INI file. 0:12 We can use this to turn on, off, or change the level of errors to display. 0:17 The problem with using only your own PHP file is, 0:23 parse errors will stop PHP from processing your code at all. 0:27 This means, PHP will never even see those settings. 0:32 These shortcomings aside, sometimes this is your best option at the time, and 0:37 you need to change settings momentarily. 0:41 Or for a particular file. 0:44 Let's explore these options in Workspaces. 0:46 We're going to go back and turn off errors with our .htaccess file. 0:50 Now we can go back to our browser and preview errors one. 1:30 We see a blank white page. 1:37 There aren't any parse errors because we're not seeing the error page. 1:39 So we can go in and turn on errors just for this page. 1:43 To tell the server we want to display errors, 1:48 we add two lines to the top of our PHP file directly below the opening PHP tag. 1:50 I've already added these lines, but they're commented out. 1:56 We'll remove the double slashes before the two lines. 2:00 The first is error reporting. 2:04 This sets error reporting to all. 2:08 The next one is the ini_set display_errors. 2:11 One means true, display errors on. 2:17 I've also included the setting for html_errors just in case. 2:22 After saving the file, let's refresh the browser. 2:28 Awesome. 2:33 We have a notice and a warning. 2:35 Notice: Undefined variable: errors, 2:36 in our errors1 on line 12. 2:41 And our warning tells us that there's an invalid argument supplied for 2:44 foreach on line 12 as well. 2:49 These are both telling us there's an error on line 12, so let's take a look. 2:53 Sure enough on line 12 we see that we have tried to loop through an array named 2:58 errors. 3:03 But the actual array we want is named error_levels. 3:04 Let's change this name. 3:09 And then we can save the page and refresh the browser. 3:11 Our errors are gone and our page is working. 3:17 Great job! 3:19 You can also suppress errors for a particular line using the @ symbol. 3:22 This can be useful if you actually want to allow things through without a warning or 3:27 if you want to hide a warning for short time. 3:33 Let's go back into work spaces and see this in action. 3:36 Let's take a look at errors3. 3:40 We have a file that has a bunch of errors. 3:44 Let's see what happens when we suppress those errors. 3:47 The first one we see is a warning, and 3:50 a fatal error that have to do with not being able to open a required file. 3:53 The call to this required file is on line ten. 3:59 If we suppress our call to required field we can suppress our errors. 4:07 There are no warnings, but the code will stop because the file is required. 4:14 So let's comment out that line for now. 4:19 Now we see a few more warnings because it failed to open files for include. 4:28 These files are found on lines 13 and 16. 4:34 Let's suppress line 13 and also the file on line 16. 4:40 Include files are not required. 4:48 So if we suppress the warnings, we can continue on. 4:50 We see that we've stopped at line 16 and displayed the message, 4:56 failed to open file. 5:00 That's because we told our script to die or 5:02 stop and display a message to the screen. 5:05 You could argue that this is a valid use for 5:08 line suppression because we are actually doing something with the error. 5:10 Let's comment out this line and see what else we have. 5:15 Finally we have three more errors. 5:24 The first is a warning telling us that we're missing argument 1 for 5:27 error function called on line 25 and define on line 18. 5:30 The second and third are both notices, 5:37 and they tell us that we have an undefined variable. 5:40 The variable bad is at line 19 and the variable error is on line 21. 5:44 We can see on line 19 that bad is actually an undefined variable. 5:52 On line 21, the error variable is the argument we're supposed to pass. 5:57 Let's suppress our function call on line 25. 6:04 If we suppress our function call, all three errors go away, 6:12 even though our variable bad has nothing to do with the missing argument. 6:15 And it's probably something that needs to be addressed. 6:20 This should give you a sample of how and when you might suppress errors and 6:23 some of the issues that they can cause. 6:27 Anytime you are suppressing errors in any form, 6:31 make sure this is actually what you want. 6:35 Warnings are there for a reason. 6:38 Line suppressions can make things especially difficult to diagnose so 6:41 use them sparingly and only when necessary. 6:45 Often exceptions are a much better way to handle these types of situations. 6:50 Don't forget to check the teacher's notes for more information. 6:55
You need to sign up for Treehouse in order to download course files.Sign up