Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Warnings and errors

From: PHP with MySQL Essential Training

Video: Warnings and errors

When we encounter an error in our code, more information that we have about that error, the easier it's going to be to resolve it. In this movie, we'll look at the Warnings and Errors that PHP generates and learn how they can help you to resolve problems. The first and most important thing is to make sure that error reporting is turned on, we can't deal with our errors if we can't see them. So we want to make sure that PHP is reporting errors as they happen to us, when we're in development. There are two ways to do this. The first is that we can set the error reporting in the php.ini file. And we saw how to do that back in the configuration chapter.

Warnings and errors

When we encounter an error in our code, more information that we have about that error, the easier it's going to be to resolve it. In this movie, we'll look at the Warnings and Errors that PHP generates and learn how they can help you to resolve problems. The first and most important thing is to make sure that error reporting is turned on, we can't deal with our errors if we can't see them. So we want to make sure that PHP is reporting errors as they happen to us, when we're in development. There are two ways to do this. The first is that we can set the error reporting in the php.ini file. And we saw how to do that back in the configuration chapter.

You just edit your php.ini file to have display errors on, error_reporting equal to whatever settings we want to have for that. And then, restart our web server so that it reloads that php.ini file. We can also set the error reporting on the fly in our PHP code. This is especially useful if we are working on a server where several people are sharing a copy of PHP. The PHP ini file might not have the settings that we want customized for our little bit of code. So in that case we can use the function ini_set to set the value as if it had been set in the ini file. So I can set display errors to be turned on and I can set error reporting using the error reporting function.

Notice that On is the word On, it's a string, but error Reporting is using a constant, E_ALL is a constant, it does not get quotes around it. There are a variety of values that we can provide to error reporting. A lot of those are detailed in the php.ini file, or on php.net. Let's just look at a couple of them. One is that we can provide E_ALL and then an upright pipe followed by E_STRICT. What this is saying is we want all errors and also the strict errors. Strict is a category of errors that was added to PHP 5, but it wasn't incorporated into ALL until 5.4.

So if you're using 5.4 and only 5.4, you can omit that second part and just have e underscore all. But if you're using 5.3 or anything before that or if you're going to be moving back and forth. Then you want to have both of them so you're seeing all errors all the time. Now we don't have to see all errors, we can selectively turn off some errors. So if we wanted to turn off the errors regarding deprecated code, then we could have E_ALL ampersand. And then the little twiddle followed by E_DEPRECATED. Deprecated is a fancy word for code that's been scheduled for removal from PHP, so in the future it's going to stop working.

I think it is generally a good idea for you to go ahead and have those errors in there so that you make sure that your code is forward compatible. And last, I just wanted to show you that you can return the current error_reporting level by just calling error_reporting with no argument. Now, what it's going to return to you is a number. It's not going to return to you one of these constants. These constants all represent numbers. And it takes those and compiles them together into a single number that it will return back to you when you call error reporting. You can get that full list of numbers on the php.net website. There's a full list of all those constants and what numbers correspond to them.

Let's take a quick peek. So here I am on that webpage. And it has all the predefined constants here, starting with Errors and Logging at the top. And you can see here is the constant, and here is the value that corresponds to it. If you scroll down here to the bottom you'll see here is e underscore all and the number that corresponds to it. What it does is it takes all of those numbers and adds them up in order to figure out what the current user level ought to be. It also gives you a description of what each of these error constants mean. This can be really useful when you want to configure your error reporting to report something less than all of the errors.

Most times I think, you probably just want to have it report all errors back to you though. And while this page lists off many different categories of errors, there's actually four main categories. Let's take a look at those and see what they mean. The first category of errors are Fatal Errors. What this means is that PHP understood what we were asking it to do, but it could not execute it. It would be like, if I asked you to walk from New York to London, you understood the English sentence that I just said, it made perfect sense. But you can't do it. When PHP encounters a fatal error, it is fatal for your code.

It means that the page of code cannot be executed, and so you don't get back anything. It's essentially saying that PHP tried and gave up. The most common cause of a fatal error is going to be calling an undefined class or a function. If you try and call a function that hasn't been defined, obviously it can't do that for us. The second category of errors are going to be Syntax Errors. In this case, PHP just plain all couldn't understand what we were asking it to do. This is usually the result of a typo, a missing semicolon, a quotation mark, a parenthesis, something like that, something that makes our code not valid.

It tried to parse it; it tried to figure out where one thing started and where one thing stopped. And it couldn't make any sense of it. And therefore, it gives us a syntax error. Often these will be reported as a syntax error, and it will say unexpected something in this file at a certain point. It was expecting to get a closing parenthesis, but instead it saw a call to define a function. Right, that was unexpected, and so it goes ahead and says oops, that's a syntax error, something's wrong here. After the error, it'll also tell you what file the error was in and the line number where the error was encountered. It's helpful if in your text editor you turn on line numbering, so that you see the line numbers running down the left side.

That'll help you identify the line very quickly. It's also very common with syntax errors for the line number to be off by one. Because if it encountered an unexpected something, the problem is probably that something happened right before it that caused it to be unexpected. Next category are Warnings, this indicates that PHP found a problem but it was able to recover from it, it was non-fatal. It understood what you were asking, it was able to execute it but it ran into a bump along the way. It indicates that you made a mistake and have a bug in your code. It wasn't a fatal error but the page is probably not correct, probably not what you intended.

An example of a warning would be if you tried to divide by zero. It would come up and give you a warning that you can't divide by zero. It'll keep processing everything after that, but whatever that mathematical operation was will have failed. You'll also get warnings if you have the incorrect number of arguments sent to a function or if you have the incorrect path to a file. Or if you don't have access permissions to access a file. In the last category are Notices. Notices are just PHP offering you advice. Think of it as PHP telling you that something smells bad. It's a good indicator of bugs and sloppy programming style. And it can also include those deprecation notices that tell you about code that's scheduled to stop working, in future versions of PHP. Your code is fine, but you really ought to think about making these adjustments. If we were going to turn off one of these four categories in development, notices is probably the one that would go first.

Now as I said at the beginning, we want to make sure that we do see these errors and warnings when we're in development. It's tremendously helpful for identifying problems, however we do not want to have errors displayed when we launch our website to the world. First of all, they're ugly. And second it would be a major security failure that would reveal far too much detail about our code. So you want to make sure that you turn off error reporting when you launch your website to the world. But just because those errors aren't on the screen anymore, doesn't mean that they don't exist. Errors are notices still generated by PHP whether they're displayed or not. And it's important to know that that takes up some time, so if you have a lot of those pesky little notices that have been generated. Well if you deploy with that PHP is still generating those notices and it does have an impact on performance for your website. So, it is better to go ahead and address them and fix them even if they are just notices about deprecations, go ahead and fix them now to speed up your website. The second point is that, chances are, those errors are still being logged by your web server.

And that's good because we definitely want to know about those errors that our users are getting on the live site. In fact, it's as important, if not more important, to know about problems that are happening on the live site. Then it is to find out about errors that are happening when we're developing. Now where your web server logs those errors can vary and can be configured, but I want to give you just a few of the places that you might look for it. Here on Mac OS X, chances are that be default, it's going to be logging to private/var/log/apahce2/error_log, and that's where you would find it. If you're on Windows in WAMP, chances are it's going to log into the WAMP folder, into Logs into php_error.log Or you can access those logs from the WAMP menu.

If you're running Apache, well then it's probably being configured inside Apache's Config file, and you could look there to find out where that's being stored. If you look in there, you do a search for Log, it should tell you where the Log file is being stored. If your version of Apache's loading in other configuration files, let's say different files for different domains that are hosted on the same sever. Well, then those might be the place to configure where the error logging occurs. You want to make sure that you check these error logs from time to time, otherwise you won't know what problems your users are encountering.

Show transcript

This video is part of

Image for PHP with MySQL Essential Training
PHP with MySQL Essential Training

131 video lessons · 36130 viewers

Kevin Skoglund
Author

 
Expand all | Collapse all
  1. 4m 8s
    1. Welcome
      1m 0s
    2. Using the exercise files
      3m 8s
  2. 15m 6s
    1. What is PHP?
      3m 52s
    2. The history of PHP
      2m 51s
    3. Why choose PHP?
      4m 10s
    4. Installation overview
      4m 13s
  3. 54m 53s
    1. Overview
      2m 33s
    2. Working with Apache Web Server
      6m 56s
    3. Changing the document root
      7m 24s
    4. Enabling PHP
      6m 16s
    5. Upgrading PHP
      3m 30s
    6. Configuring PHP
      10m 3s
    7. Installing MySQL
      5m 46s
    8. Configuring MySQL
      7m 24s
    9. Text editor
      5m 1s
  4. 31m 25s
    1. Overview
      3m 27s
    2. Installing WampServer
      5m 46s
    3. Finding the document root
      2m 24s
    4. Configuring PHP
      8m 12s
    5. Configuring MySQL
      5m 45s
    6. Text editor
      5m 51s
  5. 19m 12s
    1. Embedding PHP code on a page
      6m 43s
    2. Outputting dynamic text
      5m 55s
    3. The operational trail
      2m 27s
    4. Inserting code comments
      4m 7s
  6. 1h 18m
    1. Variables
      7m 50s
    2. Strings
      4m 38s
    3. String functions
      8m 54s
    4. Numbers part one: Integers
      6m 27s
    5. Numbers part two: Floating points
      5m 25s
    6. Arrays
      10m 0s
    7. Associative arrays
      6m 37s
    8. Array functions
      6m 33s
    9. Booleans
      3m 50s
    10. NULL and empty
      5m 15s
    11. Type juggling and casting
      8m 27s
    12. Constants
      4m 43s
  7. 27m 37s
    1. If statements
      6m 0s
    2. Else and elseif statements
      4m 16s
    3. Logical operators
      7m 30s
    4. Switch statements
      9m 51s
  8. 42m 15s
    1. While loops
      8m 41s
    2. For loops
      5m 59s
    3. Foreach loops
      8m 16s
    4. Continue
      8m 28s
    5. Break
      4m 8s
    6. Understanding array pointers
      6m 43s
  9. 37m 25s
    1. Defining functions
      8m 25s
    2. Function arguments
      5m 32s
    3. Returning values from a function
      7m 33s
    4. Multiple return values
      4m 53s
    5. Scope and global variables
      6m 2s
    6. Setting default argument values
      5m 0s
  10. 20m 18s
    1. Common problems
      3m 47s
    2. Warnings and errors
      8m 36s
    3. Debugging and troubleshooting
      7m 55s
  11. 57m 57s
    1. Links and URLs
      5m 33s
    2. Using GET values
      5m 35s
    3. Encoding GET values
      8m 41s
    4. Encoding for HTML
      9m 26s
    5. Including and requiring files
      7m 40s
    6. Modifying headers
      6m 45s
    7. Page redirection
      6m 43s
    8. Output buffering
      7m 34s
  12. 1h 3m
    1. Building forms
      7m 28s
    2. Detecting form submissions
      5m 59s
    3. Single-page form processing
      7m 57s
    4. Validating form values
      10m 40s
    5. Problems with validation logic
      9m 54s
    6. Displaying validation errors
      7m 23s
    7. Custom validation functions
      6m 28s
    8. Single-page form with validations
      7m 25s
  13. 28m 5s
    1. Working with cookies
      2m 49s
    2. Setting cookie values
      5m 55s
    3. Reading cookie values
      6m 1s
    4. Unsetting cookie values
      4m 51s
    5. Working with sessions
      8m 29s
  14. 48m 39s
    1. MySQL introduction
      6m 43s
    2. Creating a database
      7m 41s
    3. Creating a database table
      7m 42s
    4. CRUD in MySQL
      5m 48s
    5. Populating a MySQL database
      7m 32s
    6. Relational database tables
      6m 40s
    7. Populating the relational table
      6m 33s
  15. 56m 4s
    1. Database APIs in PHP
      4m 51s
    2. Connecting to MySQL with PHP
      7m 45s
    3. Retrieving data from MySQL
      8m 47s
    4. Working with retrieved data
      6m 12s
    5. Creating records with PHP
      6m 58s
    6. Updating and deleting records with PHP
      9m 6s
    7. SQL injection
      3m 5s
    8. Escaping strings for MySQL
      6m 45s
    9. Introducing prepared statements
      2m 35s
  16. 35m 58s
    1. Blueprinting the application
      7m 19s
    2. Building the CMS database
      5m 14s
    3. Establishing your work area
      4m 38s
    4. Creating and styling the first page
      4m 22s
    5. Making page assets reusable
      6m 36s
    6. Connecting the application to the database
      7m 49s
  17. 32m 49s
    1. Adding pages to the navigation subjects
      5m 58s
    2. Refactoring the navigation
      6m 7s
    3. Selecting pages from the navigation
      6m 2s
    4. Highlighting the current page
      5m 26s
    5. Moving the navigation to a function
      9m 16s
  18. 1h 45m
    1. Finding a subject in the database
      9m 48s
    2. Refactoring the page selection
      10m 52s
    3. Creating a new subject form
      6m 55s
    4. Processing form values and adding subjects
      11m 20s
    5. Passing data in the session
      9m 16s
    6. Validating form values
      9m 40s
    7. Creating an edit subject form
      8m 30s
    8. Using single-page submission
      7m 44s
    9. Deleting a subject
      9m 44s
    10. Cleaning up
      10m 37s
    11. Assignment: Pages CRUD
      4m 30s
    12. Assignment results: Pages CRUD
      6m 10s
  19. 39m 26s
    1. The public appearance
      8m 52s
    2. Using a context for conditional code
      11m 37s
    3. Adding a default subject behavior
      6m 9s
    4. The public content area
      5m 51s
    5. Protecting page visibility
      6m 57s
  20. 1h 3m
    1. User authentication overview
      4m 3s
    2. Admin CRUD
      8m 41s
    3. Encrypting passwords
      7m 26s
    4. Salting passwords
      5m 42s
    5. Adding password encryption to CMS
      11m 54s
    6. New PHP password functions
      3m 13s
    7. Creating a login system
      11m 28s
    8. Checking for authorization
      5m 48s
    9. Creating a logout page
      5m 40s
  21. 2m 4s
    1. Next steps
      2m 4s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed PHP with MySQL Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.