Whether we like it or not, once we write code we will make mistakes and errors. Therefore it is imperative we will catch and fix problems in our applications. For that php has several tools for debugging. I will talk about the basics of code debuging.
But before we start debugging we need to set the stage the right way. First we let php know that we want to display errors:
This code should be placed at the top of our code:
ini_set('display_errors','on');
error_reporting(E_ALL);
It is imperative to remember to turn this off once we are dealing with an active site, that is production inviroment.
Notice that for the moment i have set the reporting level to all, that means that i want to see errors from different levels.
Now we are ready to make and test errors.
Syntax errors:
As the name suggest these are erros that comes from the syntax, so when we forget something like the ; at the end of a php line:
$text = 'i am without a close to the statment'
print $text;
And the error we will get on the screen is this:
Parse error: syntax error, unexpected T_PRINT in C:\xampp\htdocs\site\debug.php on line 12
I am told what kind of error happened and on which line. But normally the line will be the one just above the mentioned line, since that when the error happaned.
Warning errors:
These types of errors does not stop the rendering of the page, but we still need to know about them.
The most common situations where these happen is when headers were already sent, or when we did not passed the right arguments for a function.
Lets do the latter:
function we($name,$age)
{
print $name . ':' . $age;
}
we('ron');
We sent to the function only one argument and not 2, so this is the error we get:
Warning: Missing argument 2 for we(), called in C:\xampp\htdocs\site\debug.php on line 22 and defined in C:\xampp\htdocs\site\debug.php on line 17
Very detailed and let us fix it with ease.
Notice errors:
These error too will not halt our application.
The most comment event when it happen is when we use a variable that was not defined:
print $myvar;;
And as you can axpect the error message will direct us to where is the issue, and we`ll know what to do next (well, define it:(
Notice: Undefined variable: myvar in C:\xampp\htdocs\site\debug.php on line 30
Fatal errors:
These are critical errors that will halt our application. An example will be a call to a non existant function:
myfunc('does not exists');
And the right error message will be displayed:
Fatal error: Call to undefined function myfunc() in C:\xampp\htdocs\site\debug.php on line 34
At the beggining of the tutorial i asked to display all sorts of errors, or all levels of errors, that is why i asked for E_ALL. But php let us choose what level of errors we can see and display. We can choose and mix to our needs. We could wrote this:
error_reporting(E_ALL | E_STRICT);
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
This is very flexible and let us do exactly what we want and need.
Obviously, we need to see errors on the development stage, but our application will have errors when on production mode. Therefore we need to take the error reporting to our advantage.
We could,for example, write a switch statment and for each case to what we need. lets say on Fatal errors send an email with the error message to the admin so the issue will be fixed as soon as posible.
Post new comment