Jump to content

Archived

This topic is now archived and is closed to further replies.

isaac_cm

what is the best way to debug php

Recommended Posts

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

Share this post


Link to post
Share on other sites
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!


Share this post


Link to post
Share on other sites
you can always print little checkmarks to see where and what your script does

[code=php:0]

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

[/code]

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

Share this post


Link to post
Share on other sites
I gave you a simple and effective way to debug, regardless if i use echo or print

Share this post


Link to post
Share on other sites
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

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

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

Share this post


Link to post
Share on other sites
btherl,
I did not know any thing about how you debug from command line ,
please give me more detail

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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:

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

function foo($a, $b) {
...
}[/code]

$ 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:

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

Then call your script as http://host.com/script.php?arg1=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.

Share this post


Link to post
Share on other sites
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:
[code]<?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");
}
?>[/code]

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:
[code]<?php
write_dbg($fp,print_r($somearray,true),__LINE__,$do_debug);
?>[/code]

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.