Jump to content


Photo

what is the best way to debug php


  • Please log in to reply
14 replies to this topic

#1 isaac_cm

isaac_cm
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 16 October 2006 - 10:50 PM

First I would like to thanks admins, room managers and members of this forum for they great help and fast response, that I did not find on any forum on the world

now I am a good php programmer but I wont be so good if I am always just use "echo" to debug my variables so I am asking the expert what is the best yet simple way to debug php

I found many but as you know there is alot of crap and very complicated scripts , so I need an efficient one and if it can work with dreamweaver as an add-on it will be so great

thanks alot

#2 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 16 October 2006 - 11:01 PM

The (2) most unused functions, that are the most useful functions when developing!

debug_backtrace(); // PHP 4.3^, PHP5

debug_print_backtrace(); // PHP 5 only


PHP 4, you need to use PEAR for debug_print_backtrace();

http://pear.php.net/package/PHP_Compat


me!




#3 isaac_cm

isaac_cm
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 17 October 2006 - 11:06 PM

is ther any other ways

#4 rab

rab
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts

Posted 17 October 2006 - 11:28 PM

you can always print little checkmarks to see where and what your script does


if( $file == "hi" ) {
print "$file == hi\n";
} else {
print "$file != hi\n";
}


Basically the same with MySQL's error functions, print mysql_error($fd);

#5 isaac_cm

isaac_cm
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 17 October 2006 - 11:33 PM

same as echo

#6 rab

rab
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts

Posted 17 October 2006 - 11:36 PM

I gave you a simple and effective way to debug, regardless if i use echo or print

#7 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 18 October 2006 - 03:20 AM

My standard php debugging technique is var_dump(), print_r() and print().  That's powerful enough to find 95% of my bugs.

When that fails, I try commenting out parts of code.

I also add sections like

$ret = some_function($argv[1,], $argv[2]);
var_dump($ret);
exit(0);

to the start of files, and debug them from the command line.

#8 isaac_cm

isaac_cm
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 18 October 2006 - 11:37 PM

btherl,
I did not know any thing about how you debug from command line ,
please give me more detail

#9 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 19 October 2006 - 12:00 AM

TIP

Use echo to output things the program should output.

Use print (or print_r() for arrays & objects) when outputting debug stuff. That way you can search for "print" when it's working to remove the stuff you put there solely for debugging.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#10 isaac_cm

isaac_cm
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 19 October 2006 - 01:08 AM

great to have you in my topic Barand , thanks alot

#11 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 19 October 2006 - 02:49 AM

Debugging from the command line depends on your development environment.. I am developing in linux, so to test new code I do something like this:

$ret = foo($argv[1], $argv[2]);
var_dump($ret);
exit(0);

function foo($a, $b) {
...
}

$ php code.php arg1 arg2
int(1)
$

Which tells me that foo(arg1, arg2) returns the integer 1.

If you are always accessing your script through a browser, you can do something similar like this:

$ret = foo($_GET['arg1'], $_GET['arg2']);
var_dump($ret);
exit(0);

Then call your script as http://host.com/scri...=blah&arg2=bleh

That shows you the result of just that function with those arguments.  It's a good technique to use on new functions, and functions that you suspect might not be working properly.


#12 isaac_cm

isaac_cm
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 19 October 2006 - 01:17 PM

ok, thanks alot

#13 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 19 October 2006 - 02:08 PM

Things start getting tricky when you're trying to debug php scripts that are invoked via AJAX, since the output goes back to the invoking routine, not to the browser screen. If the invoking routine is not expecting to see debugging output, you can cause more errors.

What I have started to do is to pass a debug flag via the URL, then I have this code in my scripts:
<?php
$do_debug = (isset($_GET['d']))?true:false;
$ftp = false;
if ($do_debug) {
   $mode = (file_exists('debug_trace.txt')?'a':'w';
   $fp = fopen('debug_trace.txt',$mode);
}

function write_dbg($fp,$msg,$line,$dbg=false) {
     fwrite($fp,date('Y-m-d G:i') . ' --- ' . __FILE__ . ' (' . $line . ') --- ' . $msg . "\r\n");
}
?>

Then, you can insert calls to the write_dbg() function where ever you want to debug something. For instance, if you want to see what's in an array that you've built:
<?php
write_dbg($fp,print_r($somearray,true),__LINE__,$do_debug);
?>

You can leave the code in, since it depends on invoking the script with debugging turned on, if debugging is not turned on, nothing gets written to the file. Just make sure that the debug file is writable by your web server.

Ken

#14 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 19 October 2006 - 07:29 PM

I've always thought the best bit about AJAX is the ease of debugging. Just call the invoked script as a stand-alone script and let it output to the screen.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#15 isaac_cm

isaac_cm
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 21 October 2006 - 11:58 AM

yes, but I have a problem with ajax please see my topic
"ajax is slow" and tell me your advice in "javascript" section

thanks alot guys




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users