Welcome to our forums...

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Forum Statistics

  • Forum Members:
  • Total Threads:
  • Total Posts: 6
There are 1 users currently browsing forums.
PHP Scripting PHP is a scripting language for rapid web-development. It's a popular and powerful, OOP-capable language that is used by professionals. Discuss PHP here.

Reply
  #1  
Old 01-02-2006
Mau Mau is offline
A friend
 
Join Date: Jun 2005
Location: California, USA
Age: 19
Posts: 2,956
Rep Power: 7
Mau is on a distinguished road
PHP Debugging Tips

Set Error Reporting to E_ALL
Quote:
Mau originally posted:
This can be easily done in php.ini. To do this, simply open your php.ini file and change the value of error_reporting to E_ALL. If you do not have access your PHP configuration file, you may insert this at the top of every page:
PHP Code:
error_reporting(E_ALL); 
Doing this will cause PHP to output extra errors, warnings, and notices--such as undefined variables. This can catch those little mistakes.
Check that MySQL Syntax
Quote:
Mau originally posted:
If you are having a problem with your MySQL statements, your SQL is most likely invalid. The best way to check your MySQL is to simply output your statement before you execute it. For example:
PHP Code:
$sql 'SELECT * FROM table WHERE id='.$my_id;
echo 
$sql;
$result mysql_query($sql); 
By doing this, you are verifying what is actually being sent to the MySQL server. If, for example, $my_id turns out to contain a string, you would see this during output.

If, after you make some changes to your SQL it still doesn't work, you should then try using MySQL's error message. The best way to do this is to tack mysql_error(); on to the end or your statements. For example:
PHP Code:
$result mysql_query($sql); or die(mysql_error()); 
This will output the first MySQL error the screen, which you can debug further.
Focus Your Scope
Quote:
Mau originally posted:
You should make sure that you are using the right variables in the right scope. Are you using a global variable, or a local variable? For example:
PHP Code:
$x 0;
function 
a()
{
  
$x++;
}
a();
print 
$x// prints 0! 
As much as you think this should print out 1, it actually prints 0. To fix it:
PHP Code:
$x 0;
function 
a()
{
  
$GLOBALS['x']++;
}
a();
print 
$x// prints 1! 
On a related note, are you using references? Do you intend to? You should check these.
Trace Through Your Program
Quote:
Mau originally posted:
Sometimes your code may be correct, but your logic isn't. You should do a back trace on your program to find out your mistake. Doing this is simple in PHP: You just output the contents of the key variables to the screen at critical points. You can then see for yourself what values are being passed.
Quote:
JordanF98765 originally posted:
...i find that moving a die('here'); command through the code helps me find exactly which line is responsible for strange behavior or output.
Take a Break; Think it Over
Quote:
Mau originally posted:
Sometimes you just need to take a break and walk away for an hour. Go do something different, away from the computer and programming. Usually, when you come back, you'll see your mistake and bop yourself on the head.

Check Those Silly Mistakes!
Quote:
unclekyky originally posted:
I know... Check those dang semicolons!
Quote:
Mau originally posted:
If you are getting a parse error, it means that your code is malformed. You should check thos semicolons and make sure that all quotes are placed in the proper location.
Check the Manual
Quote:
Mau originally posted:
You should check the manual at www.php.net. Type in the function that you are having trouble with and verify that you are using it correctly. This manual also contains many examples, so if you are doing something similar to the examples, check to make sure you are doing it correctly.
Ask Someone
Quote:
Mau originally posted:
If you are still having trouble, then ask! Just write your question and somebody will probably get back to you in a few days.
Further Reading

If any of you would like to share your debugging techniques, please post below. They will be added to the master list, with credit given to you.

Last edited by Mau; 03-13-2006 at 07:04 PM.
Reply With Quote
  #2  
Old 01-02-2006
The Clown
 
Join Date: Nov 2005
Location: Chicago
Age: 21
Posts: 103
Rep Power: 5
JordanF98765 is on a distinguished road
Re: PHP Debugging Tips

A very good list of techniques. To add into the Tracing Your Code section, i find that moving a die('here'); command through the code helps me find exactly which line is responsible for strange behavior or output.
Reply With Quote
  #3  
Old 01-03-2006
unclekyky's Avatar
Jovially Avuncular
 
Join Date: Sep 2004
Age: 20
Posts: 5,889
Rep Power: 11
unclekyky is on a distinguished road
Re: PHP Debugging Tips

I know... Check those dang semicolons!
Reply With Quote
  #4  
Old 01-02-2007
College Student
 
Join Date: Jan 2007
Posts: 210
Rep Power: 4
Mgccl is on a distinguished road
Re: PHP Debugging Tips

use dBug and debugConsole
also, you can try use
if(30 == $a);
instead of
if($a == 30);
so when you misspell "==" with "="
there will be an error...


if(30 == $a);

Reply With Quote
  #5  
Old 08-06-2007
MaradoX-'s Avatar
Child
 
Join Date: Aug 2007
Location: Belgium - Edegem
Age: 19
Posts: 72
Rep Power: 3
MaradoX- is on a distinguished road
Re: PHP Debugging Tips

PHP Code:
$result mysql_query($sql); or die(mysql_error()); 
isnt it supposed to be?:

PHP Code:
$result mysql_query($sql) or die(mysql_error()); 
Reply With Quote
  #6  
Old 01-04-2008
Toddler
 
Join Date: Jan 2008
Posts: 9
Rep Power: 0
priji is on a distinguished road
Re: PHP Debugging Tips

Watch for parser errors at the End of Feed (EOF). If the parser error line number is the very bottom of your script then you likely have a braces mismatch. These can be very difficult to spot if you’ve added a bunch of code without running the program. Here’s an example of a braces mismatch: <?php
$flag = 0;
$names = array(‘TDavid’,‘php-scripts.com’,);
foreach($names as $each) {
if($each == ‘TDavid’) {
$flag = 1;
if($flag == 1) {
print ‘I found TDavid’;
}
}
?>

Can you spot the brace mismatch above? It should be directly below the $flag=1; One way in coding that you can cut down on brace mismatches is always indent. Let’s rewrite the code above with indenting below:
<?php
$flag = 0;
$names = array(‘TDavid’,‘php-scripts.com’,);
foreach($names as $each) {
if($each == ‘TDavid’) {
$flag = 1;
}
if($flag == 1) {
print ‘I found TDavid’;
}
}
?>

Notice how the braces match up? If you indent your code you’ll keep braces closed. There are also some helper text editor programs that will watch for brace mismatches and alert you to this common error.
Reply With Quote


Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Tutorial for beginners YoungCoder PHP Articles 19 04-23-2006 02:31 PM
What is PHP? wizard PHP Articles 4 11-27-2005 07:38 AM
Php Syntax wizard PHP Articles 1 10-06-2005 01:53 AM
Top 10 PHP tips YoungCoder PHP Articles 7 08-16-2005 03:33 AM
PHP Manual [Chris's Version] Chrishasfun PHP Articles 0 01-02-2005 03:22 PM