Jump to content

what is the best way to debug php


isaac_cm

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
Link to comment
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);
Link to comment
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.
Link to comment
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.
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.