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