Debugging your PHP code - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: PHP The Hypertext Preprocessor (https://www.supportforums.net/forumdisplay.php?fid=21) +---- Thread: Debugging your PHP code (/showthread.php?tid=1055) |
Debugging your PHP code - Gaijin - 10-10-2009 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 http://www.php.net Wrote:The error_reporting() function sets the error_reporting directive at runtime. PHP has many levels of errors, 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.error-reporting.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 NOTE: The both steps are only recommended to be used in the develpoment process, in a live environment you should use. PHP Code: <?php 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 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 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(); 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'); 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! RE: Debugging your PHP code - Headshot - 10-10-2009 Ninja.. those stuff you do, are they hard to do? Do you have a site? Nice man.. I'm amused. RE: Debugging your PHP code - Gaijin - 10-10-2009 (10-10-2009, 12:02 PM)Headshot Wrote: Ninja.. those stuff you do, are they hard to do?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.... RE: Debugging your PHP code - Headshot - 10-10-2009 Would you PM it to me? If you don't want to, sure. I understand! RE: Debugging your PHP code - Gaijin - 10-11-2009 (10-10-2009, 12:19 PM)Headshot Wrote: Would you PM it to me? If you don't want to, sure. I understand! 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. |