isaac_cm Posted October 16, 2006 Share Posted October 16, 2006 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 worldnow 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 phpI 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 greatthanks alot Quote Link to comment Share on other sites More sharing options...
printf Posted October 16, 2006 Share Posted October 16, 2006 The (2) most unused functions, that are the most useful functions when developing!debug_backtrace(); // PHP 4.3^, PHP5debug_print_backtrace(); // PHP 5 onlyPHP 4, you need to use PEAR for debug_print_backtrace();http://pear.php.net/package/PHP_Compatme! Quote Link to comment Share on other sites More sharing options...
isaac_cm Posted October 17, 2006 Author Share Posted October 17, 2006 is ther any other ways Quote Link to comment Share on other sites More sharing options...
rab Posted October 17, 2006 Share Posted October 17, 2006 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); Quote Link to comment Share on other sites More sharing options...
isaac_cm Posted October 17, 2006 Author Share Posted October 17, 2006 same as echo Quote Link to comment Share on other sites More sharing options...
rab Posted October 17, 2006 Share Posted October 17, 2006 I gave you a simple and effective way to debug, regardless if i use echo or print Quote Link to comment Share on other sites More sharing options...
btherl Posted October 18, 2006 Share Posted October 18, 2006 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. Quote Link to comment Share on other sites More sharing options...
isaac_cm Posted October 18, 2006 Author Share Posted October 18, 2006 btherl,I did not know any thing about how you debug from command line , please give me more detail Quote Link to comment Share on other sites More sharing options...
Barand Posted October 19, 2006 Share Posted October 19, 2006 TIPUse 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. Quote Link to comment Share on other sites More sharing options...
isaac_cm Posted October 19, 2006 Author Share Posted October 19, 2006 great to have you in my topic Barand , thanks alot Quote Link to comment Share on other sites More sharing options...
btherl Posted October 19, 2006 Share Posted October 19, 2006 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 arg2int(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=blehThat 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. Quote Link to comment Share on other sites More sharing options...
isaac_cm Posted October 19, 2006 Author Share Posted October 19, 2006 ok, thanks alot Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 19, 2006 Share Posted October 19, 2006 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]<?phpwrite_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 Quote Link to comment Share on other sites More sharing options...
Barand Posted October 19, 2006 Share Posted October 19, 2006 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. Quote Link to comment Share on other sites More sharing options...
isaac_cm Posted October 21, 2006 Author Share Posted October 21, 2006 yes, but I have a problem with ajax please see my topic"ajax is slow" and tell me your advice in "javascript" sectionthanks alot guys Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.