Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Turning On and Off Errors2:52 with Alena Holligan
Instead of guessing at what went wrong, let's allow PHP to respond with the errors it encountered. We'll review the error levels and talk about how to display those errors to the screen.
Notice: PHPs way off telling you: "You probably shouldn't be doing what you're doing, but I'll let you do it anyway". Notice errors will not stop the execution of the script. An example of a notice would be when you try to access an undefined variable. Defining a variable before accessing that variable is not required, but it is good practice and could indicate there is something missing.
Warning: PHPs way off telling you: "You’re doing something wrong and it is very likely to cause errors in the future, so please fix it". As with Notice errors, Warning errors will not stop execution of the script. For example: trying to include a file that is missing, or calling a function with the incorrect number of parameters would each produce a warning.
Fatal Error: PHPs way off telling you: "You’ve done something wrong and I don't know how I'm supposed to process this script". Fatal errors are caused when PHP encounters a logical error. There is no issue with the syntax and the script is readable, however, what you’re asking the script to do can't be done. For example: Dividing by zero, trying to access a function that doesn’t exist, or running out of memory. Fatal errors DO stop the execution of the script.
Showing the Errors
There are different locations where we can control what error types are to be reported:
- In the php.ini file
- In the .htaccess file on your web server
- From your own PHP code.
Even the most seasoned professional mistypes or forgets something. 0:00 So how do we find and fix those mistakes? 0:04 In PHP there are basically three types or levels of errors. 0:08 Notice. 0:13 Notices are PHP's way of telling you, 0:14 you probably shouldn't be doing what you're doing. 0:16 But I'll let you do it anyway. 0:19 Notices will not stop the execution of the script. 0:22 An example of a notice would be when you try to access an undefined variable. 0:25 Defining a variable before accessing that variable is not required. 0:30 But it is good practice and could indicate that there is something missing. 0:35 Warnings are PHP's way of telling you, you're doing something wrong. 0:41 And it's very likely to cause errors in the future. 0:44 So please fix it. 0:47 As with notices, warnings will not stop execution of the script. 0:50 For example, trying to include a file that is missing. 0:54 Or calling a function with the incorrect number of parameters 0:58 would each produce a warning. 1:01 Fatal errors are PHP's way of telling you, you've done something wrong and 1:05 I don't know how I'm supposed to process this script. 1:09 Fatal errors are caused when PHP encounters a logical error or 1:12 a syntax error that prevents the script from being understood. 1:16 For example, dividing by zero, 1:20 trying to access a function that doesn't exist, or running out of memory. 1:23 Fatal errors do stop the execution of the script. 1:29 Php7 lets us get more specific about our fatal errors. 1:33 For example trying to divide by zero will produce a specific division by zero error. 1:37 We'll be looking at some more examples of those errors shortly. 1:44 But first we need to make sure that we can see our errors. 1:47 Well I won't be going in depth into the subject of debugging at this time. 1:51 I do want to make sure that you can see the PHP errors, 1:55 instead of showing a blank white page when something goes wrong. 1:58 There are three ways to tell the server to display errors. 2:02 In the php.ini file, in the .htaccess file on your web server or 2:05 from your own PHP code. 2:10 As an important note, we want to make sure that we do not display 2:14 these PHP system errors on a production server at any time. 2:19 Displaying error messages to your site's visitors can disclose 2:24 important information about your site setup. 2:28 And can help hackers launch a cross-site scripting attack on your site. 2:31 This can be a huge security risk. 2:36 Although I will not go in-depth into security, 2:39 I will occasionally make you aware of some of the biggest threats. 2:42 We'll take a closer look at each of these three methods of displaying errors 2:46 in the next videos. 2:50
You need to sign up for Treehouse in order to download course files.Sign up