Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Debugging your PHP code
#1
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:
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");

?>
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.
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!
Reply
#2
Ninja.. those stuff you do, are they hard to do?

Do you have a site?

Nice man.. I'm amused.
Reply
#3
(10-10-2009, 12:02 PM)Headshot Wrote: Ninja.. those stuff you do, are they hard to do?

Do you have a site?

Nice man.. I'm amused.
Thank you!
I wouldn't say that it is hard, but it depends on you and how much time you can invest in learing and developing.

Yes I do have a site, but I will not post it sorry.... Gratte
Reply
#4
Would you PM it to me? If you don't want to, sure. I understand! Smile
Reply
#5
(10-10-2009, 12:19 PM)Headshot Wrote: Would you PM it to me? If you don't want to, sure. I understand! Smile

I will but not now, I'm sorry.
The site(I'm) isn't ready to be associated with me.... on this forum or hf.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP Framework List: An Ultimate Guide to 102 PHP Frameworks for Web Developers tk-hassan 0 953 07-27-2020, 11:26 PM
Last Post: tk-hassan
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 3,688 10-10-2011, 01:00 PM
Last Post: Greyersting
  How to write good php code [Must Read] BannedPoop 19 6,579 08-30-2011, 08:28 AM
Last Post: Slash
  [CODE] Update Twitter using cURL and PHP Jamza 4 2,313 02-23-2011, 12:07 AM
Last Post: sup_hlw
  Php Anti-DDos code tsgh mike 12 3,794 02-24-2010, 01:16 AM
Last Post: Jordan L.

Forum Jump:


Users browsing this thread: 1 Guest(s)