10-10-2009, 11:40 AM
Welcome and Enjoy!
Intro
Debugging the code isn't very enjoyable process, but you will face errors when writing any type of code.
So knowing how to debug your code is very useful and Important, it will speed up your creation process, and help you write your code based on the standards..
Starting:
When writing your code, it's important to know where you made a mistake.
On your development platform you should enable error reporting.
You can set error reporting within the runtime of your code with the following:
The parameters of error_reporting are all constants, and they are used without the Quotes ("").
E_ALL is used to display all levels of errors, but it won't display E_STRICT level.
Under E_ALL fall levels like E_ERRORS, E_WARNING, E_PARSE, E_NOTICE and few others.
More information @ http://www.php.net/manual/en/function.er...orting.php
The next way is to enable error_reporting within your php.ini, that can be done with the ini_set() function.
The function ini_set updates you php.ini file, and with the above you enable error reporting.
NOTE: The both steps are only recommended to be used in the develpoment process, in a live environment
you should use.
You do not want to confuse your avrage user, and you sure don't want to give a malicious user more information about your site and code.
Types:
The error you get most is probably the Syntax Error.
As you can see in the above error message, the php code has an parse error in the line 5, "expection `'{'' " tells us what we have forget to add.
Now you can debug your code to the following:
Now the code doesn't have errors and it's debugged.
Warnings:
Warnings when occure won't halt your code like most of the errors.
They are recognized by PHP as a mistakee of the author and often appear because:
Incorrect path when calling or includeing files.
Wrong parameter passed to a function.
And Headers already sent. They appear when calling a function that needs to be called before any output of the code.
Notice:
These errors are important in tracking bugs within your code.
Often your code will work perfectly, but still has bugs in it.
In the above code we are looking if an non existent member of an array hasn't a value "true".
The result will be be a NOTICE error "Undefined Index" as stated above.
The solution to this ist to defiane a variable before using it.
Now there is a memebr "index" in our $bug array, and no Nitice errors are shown.
Last Words:
Enabling error_reporting during the deelopment process is very useful, but when publishing your code you want to disable error_reporting again.
Thank you for reading!
Intro
Debugging the code isn't very enjoyable process, but you will face errors when writing any type of code.
So knowing how to debug your code is very useful and Important, it will speed up your creation process, and help you write your code based on the standards..
Starting:
When writing your code, it's important to know where you made a mistake.
On your development platform you should enable error reporting.
You can set error reporting within the runtime of your code with the following:
PHP Code:
<?php
error_reporting(E_ALL | E_STRICT);
?>
http://www.php.net Wrote:The error_reporting() function sets the error_reporting directive at runtime. PHP has many levels of errors,
using this function sets that level for the duration (runtime) of your script.
The parameters of error_reporting are all constants, and they are used without the Quotes ("").
E_ALL is used to display all levels of errors, but it won't display E_STRICT level.
Under E_ALL fall levels like E_ERRORS, E_WARNING, E_PARSE, E_NOTICE and few others.
More information @ http://www.php.net/manual/en/function.er...orting.php
The next way is to enable error_reporting within your php.ini, that can be done with the ini_set() function.
PHP Code:
<?php
ini_set("display_errors", "On");
?>
NOTE: The both steps are only recommended to be used in the develpoment process, in a live environment
you should use.
PHP Code:
<?php
error_reporting(E_NONE);
?>
You do not want to confuse your avrage user, and you sure don't want to give a malicious user more information about your site and code.
Types:
The error you get most is probably the Syntax Error.
Code:
Parse error: parse error, expecting `'{'' in E:\host\test.php on line 5
PHP Code:
<?php
function test()
//
}
?>
As you can see in the above error message, the php code has an parse error in the line 5, "expection `'{'' " tells us what we have forget to add.
Now you can debug your code to the following:
PHP Code:
<?php
function test() {
//
}
?>
Now the code doesn't have errors and it's debugged.
Warnings:
Warnings when occure won't halt your code like most of the errors.
They are recognized by PHP as a mistakee of the author and often appear because:
Incorrect path when calling or includeing files.
Wrong parameter passed to a function.
And Headers already sent. They appear when calling a function that needs to be called before any output of the code.
Notice:
These errors are important in tracking bugs within your code.
Often your code will work perfectly, but still has bugs in it.
Code:
Notice: Undefined index: index in E:\host\test.php on line 5
PHP Code:
$bug = array();
if(!$bug['index'] == 'true') {
$bug['index'] = 'true';
}
In the above code we are looking if an non existent member of an array hasn't a value "true".
The result will be be a NOTICE error "Undefined Index" as stated above.
The solution to this ist to defiane a variable before using it.
PHP Code:
$bug = array('index' => 'false');
if(!$bug['index'] == 'true') {
$bug['index'] = 'true';
}
Now there is a memebr "index" in our $bug array, and no Nitice errors are shown.
Last Words:
Enabling error_reporting during the deelopment process is very useful, but when publishing your code you want to disable error_reporting again.
Thank you for reading!