Tutorials

Debugging: A Beginner's guide

by Ben Smithers on Jun 9, 2008 5:47:39 PM

Fatal Errors, Warnings and Notices

Now would be a good time to mention another common mistake: the use of error suppression during development. PHP allows you to quietly ignore errors (other than syntactical) by placing the @ symbol at the start of the line. As with the display of errors, this is handy for a production server –- but when you're trying to find out why something doesn't work, it's worse than useless.

Fatal errors

Fatal errors are caused when PHP understands what you've written, however what you're asking it to do can't be done. Common examples include trying to use a function or class that hasn't been defined. Fortunately, the error message generated should be nice and obvious:

Fatal error: Call to undefined function foobar() in C:\wamp\www\phpfreaks.php on line 2

Warnings

Warnings refer to things which PHP can cope with, but it's likely you didn't intend them. Common causes include trying to include a missing file or using the incorrect number of parameters in a function. Again, the error message should help you out.

Probably the most common warning sent is the one that looks something like this:

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\phpfreaks.php:2) in C:\wamp\www\phpfreaks.php on line 3

Or like this:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\wamp\www\phpfreaks.php:2) in C:\wamp\www\phpfreaks.php on line 3

Both of these are caused by the same thing: you cannot change headers -- which includes changing location or setting a cookie/session -- after you have sent any content to the browser (ok smarty-pants so you can with output buffering, but that's a different subject for a different day).

Note: Output includes whitespace. So you can't even send a space or blankline before you send a header. This catches people out sometimes, particularly when including files. You might have some whitespace after your closing PHP tag which is included in another file.

Notices

Finally, we have notices. So often ignored, but can actually help you track an awful lot of errors. A notice is generally given for something which is undefined: a variable, constant or element of an array.

Before you see them, however, you'll need to make sure that PHP is setup to report them. This requires a further change of your php.ini. I recommend changing the error_reporting setting to E_ALL. This can also be done at the top of your script with:

For more on error reporting and the relevant options, see the manual.

If you've just turned notices on, you may have just got a truckload of error messages. PHP can cope with notices, hence why your script was working before. But as mentioned, notices are incredibly useful for tracking typos by warning you of an undeclared variable. Common notices come from unquoted strings being used as the key of an array, such as:

This is because, without quotes, key is a constant and not a string. Whenever PHP encounters undefined constants, it assumes that it's a string, so it can cope. However, best practice is to place quotes around the key if it is a string.

Note: It's perfectly acceptable to use an unquoted string as your key inside double quotes, such as:

That about wraps it up for our first two types of error, so we'll move onto database errors on page 5.

Comments

One small note worth adding: setting display_errors at runtime (ini_set()) won't work if the script contains fatal errors (like a parse error) because it will never get executed.

1. philipolson on Jun 10, 2008 2:09:56 PM

That's a good point there. I'll add a note in to that effect.

2. Ben Smithers on Jun 10, 2008 5:13:00 PM

You can also set error reporting in the .htaccess file if you have access to that and not your php.ini file. Here's what you need to put in the .htaccess file

php_flag display_errors on
php_value error_reporting 2047

// Or whatever error reporting level you want to use - I use 2047.

3. anthropos9 on Jun 10, 2008 6:55:58 PM

Re comment #1: Did you check that what you said is true? Setting it at runtine does work (providing you do it before any errors occur).

4. Daniel Egeberg on Jun 11, 2008 12:54:49 AM

With a syntax error Daniel? Not in my tests. With error_reporting turned off, this prodces a blank page:

5. Ben Smithers on Jun 11, 2008 1:40:02 AM

Hmm... I tested using

That does not output any errors. I just figured it would be the same type of fatal error as a syntax error would.

6. Daniel Egeberg on Jun 11, 2008 2:39:08 AM

Yeah, but then i think that makes sense. If there's a parse error, then the PHP engine wont be able make sense of the code; so it wont be able to understand that it's supposed to make a modification to the php.ini. Whereas with something like a fatal error for an undefined function, PHP can understand what it's being asked to do, just can't do it.

Also; valid point regarding .htaccess anthopos9. I'm not sure i'll add it in however, as it might just confuse the issue.

7. Ben Smithers on Jun 11, 2008 7:22:07 AM

Ben, with the htaccess thing... "php_flag display_errors on
php_value error_reporting 2047"

Right now theres nothing in the htaccess file but it writes errors to "error_log".
If I added in what you said would it fix it?

8. Stephen on Jun 15, 2008 8:31:49 PM

Well, it would turn turn display_errors On and it would set your error_reporting to 2047 which is all errors except recoverable. So if that's what you meant by 'fix it' then yes, i guess it would.

You might need to clarify, however.

9. Ben Smithers on Jun 16, 2008 3:51:06 PM

I'm new to php and I am reading PHP 5 for Dummies, I'm at chapter 3 creating you first PHP script. The script I'm using is
<html>
<head><title>Hello World Script</title></head>
<body>
<?php
echo “<p>Hello World!</p>”
?>
</body>
</html>
I saved it as helloworld.php on my localhost and even on my web server and I keep getting the same message:
Parse error: syntax error, unexpected '>' in C:\wamp\www\Test\helloworld.php on line 5
I have no idea whats wrong with it as I have just copied and pasted from the ebook!!
Please could someone help as I can't work it out?

10. Bleakers on Jun 19, 2008 8:51:48 AM

It appears you're using some sort of curly quotes instead of the regular straight ones. It's probably printed like that in your ebook. Try to type it manually and run it again.

11. Daniel Egeberg on Jun 19, 2008 1:17:22 PM

Thank you very much Daniel. That has fix the problem.

12. Bleakers on Jun 20, 2008 6:40:16 AM

As an addendum to the section on logic errors, an extremely handy line of code for figuring out what variables have what values (without having to exhaustively echo them) is the following:

This will exit the script - you can just as easily use echo. As a warning, it only works within the scope from which it is called (ie. from within functions only the local scope is returned), and from the global scope, you'll get all superglobals as well. This can flood you with data that you don't need - in this case, feel free to trim it down by specifying particular arrays in place of get_defined_vars().

13. akitchin on Aug 21, 2008 6:12:02 PM
Login or register to post a comment.