php.w3clubs.com

PHP tutorials and articles

 

Errors and mishaps

Sooner rather than later, you'll create some piece of code with an error in it. This is perfectly normal. In fact, when creating software, much more time is spent maintaining the code, rather than writing it. Part of the maintenance is adding new features, but part is also finding and removing errors. Let's see what we have in PHP when it comes to errors.

Syntax error

When you type in some invalid code (meaning code that doesn't follow PHP's syntax rules) and then you execute it, you'll get an immediate feedback in the browser. In the following example, there's a missing semi-colon at the end of the second echo line:

<?php
echo 'Hello!';
echo 'Hi, '
echo 'there';
?>

If you load this page, you'll get the error message:

Parse error: syntax error, unexpected T_ECHO, expecting ',' or ';' in C:\mywebstuff\htdocs\php-tutorial\errors.php on line 4

This is a syntax error (also called a parse error), which means a pretty bad error, so bad that no part of the script works. No lines before, nor after the one with an error will be executed. Luckily, that's an in-your-face error, so there's no way you can miss it. Most of the time the error descriptions give you enough information so that you can fix the error quickly.

A decent text editor with syntax highlighting can help you spot syntax errors while you type. If your editor doesn't highlight the "dte" when you type dte('Y-m-d'), obviously you typed "date" wrong.

The error description

Let's read together what the error message above actually meant. The message said that there's an error on line 4, although the fix to the problem is to add a semi-colon on line 3. Why is that? Well, technically there's nothing wrong with line 3, the missing semi-colon could've been placed on the next line and that would still be valid. But that was not our case, we had an "echo", another statement, on the 4th line. And this is what the PHP parser complains when it reaches line #4: "Hey, I can accept a semi-colon or a comma here, why are you giving me an echo for?! I can't work with an echo here!" So that's why the error says exactly that - on the 4th line, a comma or a semi-colon was expected, but an echo (T_ECHO) was given.

Not an Error, but a Warning

Now let's add the missing semi-colon to the code above and also add two more lines, calling the date function with and without parameters.

<?php
echo 'Hello!';
echo 'Hi, ';
echo 'there';
echo date();
echo date('Y-m-d');
?>

The result will be not an error but a warning:

Hello!Hi, there
Warning: date() expects at least 1 parameter, 0 given in C:\mywebstuff\htdocs\php-tutorial\errors.php on line 5
2007-09-07

So the error-free parts of the script are executed OK, but there is this ugly message where the error is. The message says that on line 5 we used the function date and we didn't pass any parameters to it, but we should've, because the function requires at least one parameter.

Error reporting level

There are different levels at which something can go wrong. And you can control which errors are displayed in the page and which are not. Enters the function error_reporting().

If we don't fix the error above but we set the error reporting level so that no errors are displayed, we might never know that there is an error.

error_reporting(E_NONE);
echo 'Hello!';
echo 'Hi, ';
echo 'there';
echo date();
echo date('Y-m-d');

This will simply give us "Hello!Hi, there2007-09-07" and no errors reported.

You cannot hide syntax error this way though, because when there's a syntax error, PHP cannot make sense of your code and doesn't even understand that you're trying to set the error reporting.

The best thing to do during the development phase is to set the error reporting to E_ALL.

error_reporting(E_ALL);

This will display more errors than the default PHP level, but it's good for you as a programer to know exactly what's happening. E_ALL will also show the so called "notices". A notice is even less of an error than the warning, so it's something you could ignore, but a sign of a good quality PHP code is that it works and shows no errors even when the error reporting level is bumped up to E_ALL.

When your site goes live though, it's better to have error reporting set to E_NONE, so that:

  • the users are not bothered with errors that may not be critical
  • they don't see some secret information such as the actual path on the server where the file is located

Error log

Even if you don't display errors to the users on the page, it's still a good idea to log them to a file and keep an eye on this file. One way to do this is to enable the error logging built into PHP, using the ini_set() function like this:

error_reporting(E_NONE);
ini_set('log_errors','On');
//ini_set('error_log', 'C:\\mywebstuff\\htdocs\\php-tutorial\\errors.txt');
echo 'Hello!';
echo 'Hi, ';
echo 'there';
echo date();
echo date('Y-m-d');

The way the code is given above, the errors will be logged to the default location - the Apache error log, which in our setup is located at C:\xampplite\apache\logs\error.log. If you uncomment the commented line above, you'll set the errors to be logged to a custom file location.

ini_set() is a function that allows you to play with PHP configuration in runtime, There are other ways to customize PHP as you'll see in further articles.

Suppressing individual errors

When you use the symbol @ you can hide an error even with error reporting set to E_ALL.

For example:

echo date();
echo date();
echo date();

will give you three error messages, but the following will give you only two, because the second line has the @ sign before an the function that would otherwise print an error.

echo date();
@echo date();
echo @date();

« to the PHP tutorial table of contents

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by WordPress, theme by ♣w3clubs.com, based on Kubrik and Sancta Simplicitas