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.
So how do we find and fix those mistakes?
In PHP there are basically three types or levels of errors.
Notices are PHP's way of telling you,
you probably shouldn't be doing what you're doing.
But I'll let you do it anyway.
Notices 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 that there is something missing.
Warnings are PHP's way of telling you, you're doing something wrong.
And it's very likely to cause errors in the future.
So please fix it.
As with notices, warnings 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 errors are PHP's way of 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 or
a syntax error that prevents the script from being understood.
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.
Php7 lets us get more specific about our fatal errors.
For example trying to divide by zero will produce a specific division by zero error.
We'll be looking at some more examples of those errors shortly.
But first we need to make sure that we can see our errors.
Well I won't be going in depth into the subject of debugging at this time.
I do want to make sure that you can see the PHP errors,
instead of showing a blank white page when something goes wrong.
There are three ways to tell the server to display errors.
In the php.ini file, in the .htaccess file on your web server or
from your own PHP code.
As an important note, we want to make sure that we do not display
these PHP system errors on a production server at any time.
Displaying error messages to your site's visitors can disclose
important information about your site setup.
And can help hackers launch a cross-site scripting attack on your site.
This can be a huge security risk.
Although I will not go in-depth into security,
I will occasionally make you aware of some of the biggest threats.
We'll take a closer look at each of these three methods of displaying errors
in the next videos.
You need to sign up for Treehouse in order to download course files.Sign up