Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to PHP
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Bookmark and Share
Lecture Comments (4)

0 answers

Post by dave smith on August 3, 2015

can you please teach javascript as well? the javascript course on educator is not structured like your course. please!

2 answers

Last reply by: marvin evins
Tue Nov 13, 2012 5:43 PM

Post by Senghuot Lim on December 25, 2011

where can i find the php.ini file in my macbook pro?

Error Handling

Error Handling

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Lesson Overview 0:09
    • Lesson Overview
  • Error Handling in PHP 0:41
    • Error Handling in PHP
    • Coding Example
  • error_reporting() Function 7:02
    • error_reporting() Function
    • Coding Example
  • Additional Error Directives 9:02
    • display_errors
    • log_errors
    • error_log
    • track_errors
    • Coding Examples
  • Error Control Operator 13:38
    • Error Control Operator & Coding Example
  • Homework Challenge 16:19
    • Homework Challenge
  • Homework Challenge (cont.) 17:58
    • Homework Challenge (cont.)

Transcription: Error Handling

Hello again, and welcome back to's Introduction to PHP course.0000

In today's lesson, we are going to be covering the topic of error handling in PHP.0005

Specifically, we are going to talk about how PHP handles errors by classifying them according to different levels, and how it reports them.0011

We're going to talk about error reporting function, which is a way of managing how errors are reported in PHP.0019

We are going to talk about a number of different configuration directives within php.ini that affect how errors are handled.0026

And we are also going to introduce a new operator, known as the error control operator, that relates to error handling in PHP.0033

PHP classifies errors according to different levels of severity.0043

And then, what you can do is set the level of severity that you want to be made aware of for a certain error.0048

So, you might want to be made aware of any severe errors, or maybe you want to be made aware of errors and warnings.0055

PHP allows you to set that level, and it also allows you to set how you want to be made aware of it.0062

For example, you could have it output to your browser, or you could have it output into a log file.0070

And the way that you set the level that you want to see in PHP is using this directive called error_reporting.0075

It's a configuration directive in php.ini, and what it does is: it globally sets the error level that you want reported 0083

for all of your scripts running in a particular PHP configuration.0090

And what you do is: you set that configuration directive to a number of different error level constants, which can be found at this link here.0095

If we go and look at this page (it's part of the PHP website), you can see that there are different constants,0116

which are...for example, the first one defined is E_ERROR; the constants are integer values.0118

And then, PHP has different constants for different levels of errors.0123

For example, E_ERRORS is for fatal runtime errors, so if you specify error reporting directive to be set to this,0128

then all the fatal runtime errors will be reported.0136

There are other ones, like E_WARNING, which is sort of a step down from error; it's maybe nonfatal errors, but something that you should know about.0139

E_NOTICE is the next level down, which is even less severe errors that happen.0148

And as you can see, there are a number of different constants you can use 0154

in the error reporting configuration directives to set the level of errors that you want to see.0160

Down here, just to mention, is E_ALL, which shows all of the different errors and warnings; it is commonly used in development,0167

because you want to be made aware of any errors that are occurring.0176

The way that the error reporting directive works is by performing bitwise operations on these different error constants.0180

For example (and I'll explain what that means in a second), if we go and look at the php.ini file,0194

and we go down a little way and find the error reporting directive, this is an example of a bitwise operation on two error reporting constants.0200

We have the error reporting constant E_ALL and the error reporting constant E_STRICT.0213

This right here is known as the bitwise or operator; what it does is: it says, 0218

"We want to see you report all errors, and then also, in addition, any messages that are reported by E_STRICT."0223

E_STRICT provides additional information to you that is not necessarily errors, but that is useful during development.0229

We are not going to go over bitwise operators in detail here.0236

If you want to find out more information, we are going to include a link in the Quick Notes to the bitwise operators on

But you can also find, within the php.ini file, in this Error handling and logging talks a lot about0248

the different error level constants that are available.0256

And it talks about how you can use these bitwise operators to customize what error levels you want to be made aware of.0259

For example, this right here is the E_ALL constant, and it is running a bitwise operation known as the and operation0270

on the bitwise operation not of the E_NOTICE constant.0284

What that is essentially saying is, "I want to be made aware of all errors, except" and this means to not...0292

"I don't want to be made aware of errors that are of the notice level"--so, it is saying everything but the notice level.0298

These are examples of how you can use these bitwise operators.0305

If we go and look at a script we have written called errorHandling.php, it is going to generate an error.0311

And in this case, we have created a variable called divideByZero, and we are going to have it be the results0322

of a division operation that divides by 0, which in PHP and most programming languages is not allowed, and it is going to generate a warning.0327

So, if we have this script, and we go and take a look at it (let me just refresh it), you can see that we get a warning.0335

It's of the error level warning; it is saying division by 0 occurred on line 14.0343

If we go back and look at errorHandling.php, see, on line 14, we divided by 0.0348

Well, if we want to, for example, configure our PHP setup so that it doesn't show warning messages,0354

which are configured by the E_WARNING constant, we can edit our php.ini file to change that.0360

What we are going to do is: we are going to say we want to show all of the errors, except we don't 0369

(and this is the not operator) want to see the warning errors.0374

If we save this, and then (because we are changing the configuration file, we need to restart Apache) if we go ahead and restart Apache,0379

and view our page again, you should see, because this error is of the E_WARNING type, it should disappear.0387

And as you can see, it does; let me just go back and change that back to the default value.0395

This is the default value that we have been using for development, and this is the default value that XAMPP sets in php.ini.0403

And we go ahead and restart Apache and make sure that the error shows up again; and we refresh the page; we can see that we get the warning again.0410

Now, one thing that PHP also provides is a built-in function known as the error reporting function.0423

It is specified here by error_reporting, and then parentheses.0431

And what it does is: it allows you to set, on a script-by-script level, or at the script level, the error reporting level that you want to be set.0436

So, for example, if you have your php.ini file, if you have the error reporting directive in there set to show all of the errors,0445

you can use the error reporting function on any particular script to...0455

For example, in this case down here, if we included this function call in our script, we could say, "We want to see all of the errors,0460

except for the warning errors," like we have just done.0466

And what that does is: whatever script we call that in, those are the only errors that are going to be reported for that script.0468

And so, what it does is: it allows you to override the global PHP configuration to set the error level to whichever you want.0475

For example, we can mimic what we have just done by changing the php.ini file in our script by adding this error reporting function.0485

And again, this is a function that is built into PHP.0497

And if we set it to "We want to see all errors except E_WARNING errors," and we save it, and then we go ahead and run our script,0500

which previously showed this error, and we run it now, because we have changed the error reporting level for this script,0514

using the error reporting function--we are not going to get the error anymore.0520

As we can see, the error disappears.0524

Let me go ahead and just get rid of this, so we can see the error again.0527

And you can see that, by eliminating that, we go back to the global setting for the error level, and our warning appears again.0533

There are a couple of other additional configuration directives you can use in php.ini to configure how error reporting is handled in PHP.0544

One of them that we are going to talk about is the display_errors configuration.0554

And basically, that is something that you set to either on or off; and that says whether you want the errors reported0558

to standard output, or (because we are developing web applications) if it is going to be output to the web page or not.0563

So, for example, when we looked at our last file, and we saw the divideByZero warning output to our web browser,0568

that was an example of display_errors being on.0575

There is also the ability to log errors to a log file, and there is a configuration directive for that called log_errors.0577

That is something that gets set to on or off as well; and that is something that lets PHP know whether you want to log your errors to file.0584

Something that goes hand-in-hand with the log_errors directive is the error_log directive.0593

What you set that to is the file name of the error log that you want the errors logged to.0599

So, log_errors turns on the logging, and then error_log specifies where you want those errors to be logged to.0605

There is also a track_errors configuration directive, and that determines whether a predefined variable called php_errormsg is made available to scripts.0614

We are going to talk about that a little bit more when we get to the error control operator.0626

If we go and go back to our php.ini file...and let's go ahead and turn the display_errors off, so you can see how that works...0631

if we turn this to off, and we restart our web server, whereas we previously saw this error, when we refresh the page,0640

we are not going to see it anymore, because the errors have been turned off.0651

Now, what we are going to do is go down to the log_errors directive, which is already turned on by default.0654

And what we need to do is set the error_log directive to the file where we want the errors logged to.0660

And if we scroll down, just a little bit further down in the php.ini file, here is the error_log directive.0667

And right now, you can see, by default, the XAMPP configuration has it log to this log file here,0672

which is a LINUX file path; and for that reason, it is not working in our Windows configuration.0679

So, if we comment that out, and go up and uncomment the line before it, which creates the error log file php_errors.log,0687

what that is going to do is: when we have any errors, now, it is going to log them to this file.0700

And the way this works is: because no path is specified, it is going to create this log file in whatever directory,0703

where we're running the script and it has errors.0710

So, now that we have added this, we save it and go ahead and restart our Apache server.0713

You can go ahead and look: this is the directory where the error handling script is.0722

As you can see right now, there is only the error handling script in there.0725

Now, if we go and reload the page, we are not going to see errors displayed, because we have turned display_errors to off.0728

But if we go and look at our directory, where the script is actually located, we can see that a php_errors.log file is generated.0736

And if we go ahead and open it up in PSPAD, we can see the error that is normally displayed.0744

It says "PHP Warning--division by zero," and then it goes on and outputs the rest of the message, saying it happened on line 16, and so forth.0749

That is an example of how the error logging works in PHP.0758

I'm going to go ahead and turn that back off and set it back to the default value that XAMPP had set up,0763

and then go ahead and also turn display_errors back on; and we'll set this value to on.0770

And when we go ahead and restart our browser, and we view the page, what we should see is that...0779

Well, first of all, let's delete this error_log file; now that we set log_errors to off, and display_errors to on,0788

we should see the error displayed on our screen, and we also should not see an error log file show up in this directory.0795

So, if we go ahead and refresh our page, we can see that warning message was output.0802

If we look back at our script directory, we can see, if I refresh it, there is no error log that was output.0808

Now, I want to talk about a new operator that we are going to introduce, called the error control operator.0820

It is an operator that is specified by the @ symbol, and what it is used for is to suppress 0825

any error messages that are generated by a particular operation or expression.0831

And the @ operator is a unary operator, and what it does is: it operates on whatever operand it prepends.0838

So, for example, in our script, we had this divideByZero variable; we set it equal to 1 divided by 0.0845

If we append this error control operator to the beginning of our variable, the error that is going to be generated0853

by this, this warning message of divideByZero, is going to be suppressed.0859

So, right now, we can see, if we refresh the page, that we are getting the warning about dividing by 0.0864

And if we go back to our script, and we add this error suppression operator to the beginning of the line,0870

we should see that the error is no longer going to appear; it is going to be suppressed.0879

And if we refresh the page, you can see: that, in fact, is true.0883

Now, the one thing that we had mentioned before is: there is a track_errors directive in php.ini, which can be set to on or off.0890

When that is set to on, it makes this predefined variable php_errormsg available.0897

What it does is: every time an error occurs, it stores an error message within this variable here.0905

You can use that variable to output it to your web page, or to wherever you want to output it to.0911

One thing to note is that it only stores the error message of the last error that occurred.0920

So, if you use this suppression operator, the error control operator, to suppress an error, 0924

you want to go ahead and output this error message in php_errormsg; you want to do that as soon as possible,0931

because any other error that occurs will overwrite the error message.0939

If we go back to our error handling page, we have suppressed the error.0942

And if we go ahead and echo this predefined variable, because our track_errors directive is set to on..and we go ahead and echo that...0947

now, instead of having no error output, we should get out what is reported by this error handling string.0960

And if we refresh the page, we see a message that says "division by zero."0967

That is the error message that was stored in the php_errormsg predefined variable.0972

So now, I just want to talk about the homework challenge for this lecture.0981

I just want to have you go through and mimic all of the different things that we have done today,0985

because it is going to give you experience with the different php.ini configuration directives.0989

It is going to give you experience using the error reporting function, which, again, allows you to adjust the error level on a script-by-script basis.0994

And it is also going to let you see how you can log errors, display errors, and also how the error control operator works in action.1001

The first thing I will have you do is create a script that is going to generate, like we did in our example, this divideByZero warning,1010

which, if you remember, is of the E_WARNING error class, and load the page to make sure that you are getting the error.1018

Then, I want you to go ahead and alter the error reporting directive in php.ini, as we did, so that the error doesn't show up.1028

And make sure you restart Apache and reload the page in the browser to make sure that the error doesn't occur.1035

Then, go ahead and direct the error reporting back to its default value, and reload the page to confirm that the error shows up again.1041

Then, like we did in our example, I want you to use the error reporting function to get the warning message to not be displayed on the script level.1050

So, use the error reporting function; reload the page to make sure the error doesn't show up; 1059

and then go ahead and get rid of the error reporting function, so that you can see that the error appears again.1063

Then, go ahead and set the display_errors directive to off, and restart Apache, so you can see how the errors aren't being displayed.1070

And then, I'm going to have you set the log_errors directive to on, and then specify a log file.1079

You can specify whatever log file you want; and reload the script, and verify that, because display is turned off, it is not being displayed, 1087

and also that the log file has been created, and that the warning was logged to your error file.1094

Go ahead and undo the process; turn error logging back off; turn display_errors back on.1101

Reload the page; make sure that the display is showing the error; and also confirm that your log file that you had before1107

doesn't have an update...well, 1) if you deleted it, that it has not been recreated, and 2) that if you hadn't deleted it, that it doesn't show the error again.1115

Then, to practice using the error control operator, go ahead and prepend the add operator to your divideByZero variable.1124

View the page to make sure the error doesn't show up, and then go ahead 1132

and use the php_errormsg variable to output that division by 0 error, so you can get practice using that.1136

That ends today's lesson; thank you for watching look forward to seeing you next time.1144