1. Errors in your php script
Nobody is perfect, so you will get errors in your php script. For any developer this means you must find and fix the error. There are a lot of techniques which may help like the functions var_dump and debug_print_backtrace, even debugger support.
2. Showing the error messages
To fix an error the error must be find somehow. This can't be possible
sometimes if the error messages aren't shown. Depending on the php installation
and configuration only some kinds of error messages are shown. You
can change the types to shown with the error_reporting
function or the setting error_reporting in your
php.ini file. To show all error messages set this value
to E_ALL
<?php
error_reporting(E_ALL);
?>
There is a setting called display_errors which enables/disables
error message at all. During developing this setting should be enabled, but
during production this setting should be disabled for security reason.
There isn't a predefined function for changing this setting so we must
use the ini_set function instead.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
As all error messages are shown we can fix them.
3. Syntax error
Syntax errors are the easiest errors to fix as they prevent from running the script at all. Before the php script is running the php interpreter transforms the php code into tokens.
<?php
/**
* Start of the script
*/
$i = 10;
do {
echo $i."\n";
$i = floor($i/2);
} while ($i)
echo 'i is now '.$i;
?>
This php script is transformed in the following list of tokes. In php there is just a list but we use the same indent as the php code.
T_OPEN_TAG
T_DOC_COMMENT T_WHITESPACE
T_VARIABLE T_WHITESPACE = T_WHITESPACE T_LNUMBER ; T_WHITESPACE
T_DO T_WHITESPACE { T_WHITESPACE
T_ECHO T_WHITESPACE T_VARIABLE . T_CONSTANT_ENCAPSED_STRING ; T_WHITESPACE
T_VARIABLE T_WHITESPACE = T_WHITESPACE T_STRING ( T_VARIABLE / T_LNUMBER ) ; T_WHITESPACE
} T_WHITESPACE T_WHILE T_WHITESPACE ( T_VARIABLE ) T_WHITESPACE
T_ECHO T_WHITESPACE T_CONSTANT_ENCAPSED_STRING . T_VARIABLE ; T_WHITESPACE
T_CLOSE_TAG
For a better readability we delete all T_WHITESPACE tokens as they are ignored anyway.
They come from all newlines and spaces inside your code.
T_OPEN_TAG
T_DOC_COMMENT
T_VARIABLE = T_LNUMBER ;
T_DO {
T_ECHO T_VARIABLE . T_CONSTANT_ENCAPSED_STRING ;
T_VARIABLE = T_STRING ( T_VARIABLE / T_LNUMBER ) ;
} T_WHILE ( T_VARIABLE )
T_ECHO T_CONSTANT_ENCAPSED_STRING . T_VARIABLE ;
T_CLOSE_TAG
The php interpreter is calculating with this list of tokens. It checks if
all tokes are at the correct location and if they fit together. The statement
$i = 10; is translated into T_VARIABLE = T_LNUMBER ; (without
any T_WHITESPACE tokens). PHP check if this is a valid statement, which is
true for this example.
If we run our script we get the following error message.
Parse error: syntax error, unexpected T_ECHO, expecting ';' in FILENAME on line 10
The php interpreter don't like line 10, however it looks okay for us. The problem is
at line 9 as the semicolon is missing after the do-while loop. The php
interpreter read all tokens until that point and expect after the closing parenthese )
a semikolon (see expecting ';'). Now he finds first a T_WHITESPACE token
(which is ignored anyway). But then it founds the T_ECHO token from echo. This
is absolutly not expected (see unexpected T_ECHO) and stops which the execution of the script
with a parse error. The list of expected tokens are truncated as they may be following
more than one token. Listing all tokens wouldn't help at all. Programmers know how to code and they
want to know whats wrong and not what may be right.
The line number mentioned only the line in which the error occurs, but may not the reason for the error. There are some places where you should search for the error.
-
In most cases the error is one line above. Beginners often miss the semicolon. For the php interpreter the current statement isn't finished yet and he expect more code which normaly fails.
-
In some cases the error is really in the given line number. You can write your whole php code in one line, but this will help noone...
-
In rare cases the error is much more lines above. Sometimes you miss a string delimiter (
'or") and php reads the following php code as part of the string. He fails at this at the next beginning string delimiter (which is maybe a new string) or if he reaches the file end. Then you get an error message likeParse error: syntax error, unexpected $end in FILENAME on LASTLINE, although the last line is (the?>) is not the reason for the error.
4. Understanding error messages
Beside syntax errors there can be of course normal errors like
dividing by 0. The statement $x = $y / $a; is valid, however it can lead to
an error message Warning: Division by Zero in FILE on LINE. All the
error messages got the same layout. They begins with a classification of the
error message. It can be a single notice but also a
fatal error which stops the php script. However, all
errors must be fixed. It doesn't help if you lower the error_reporting level, the
error still exists.
After the classification the error message is followed. It descripe what
happends or what didn't worked. These can only read with knowledge.
An error Undefined index '0' is easy, but
Warning: Cannot modify header information - headers already sent by
(output started at FILE:LINE) in FILE on line LINE maybe not.
The mentioned filename shows where the error occured, but may be not the reason for the error (like a variable set in an other file). A common error is to fix the wrong file and watching how the error don't go away.
5. Finding semantic errors
If php shows an error message it is easy to find and fix them. But there are errors which cannot be find with php but still are errors. These semantic errors cannot be find by php as he is running the php code and don't think about it.
To check the flow of you php script you should use proper indent. If you indent your code you may resolve logical errors already. To check e.g. if an if statement is entered you should use echo or var_dump for debug lines.
<?php
echo 'before the if';
if (condition) {
echo 'I am inside the if statement';
do_this();
} else {
echo 'I am inside the else';
do_that();
}
echo 'after the if';
?>
If you get the output before the ifI am inside the ifstatementafter the if
you first noticed you miss html newlines <br/> or
php newlines with \n. But you also noticed the if
statement is entered. If this isn't correct in your case you should check
the condition, best with var_dump. You can check
every variable with var_dump.
<?php
while ($i > $j) {
var_dump($i, $j);
do_this($i);
do_that($j);
$i += $j;
}
?>
This can also be used to output sql queries which are send to a database like MySQL.
<?php
var_dump($sql);
mysql_query($sql) or die(mysql_error());
?>