note 94702 modified in debugger by danbrown

From: Date: Thu, 19 Nov 2009 16:20:36 +0000
Subject: note 94702 modified in debugger by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-163076@lists.php.net to get a copy of this message
I always include debugging code in all may major projects. I use an object (e.g. $CFG) to store the settings the control the code's behavior, which includes a $CFG->debug variable to control the level of debugging verbosity. I set it to 0 before rollout, which ensures that even if some debugging code was left in accidentally, this will be deactivated and not spew debugging information in a live website. I use the following functions to facilitate debugging messages. They live in a single code module which I only need include() to use it. Note the global reference to $CFG: <?php // Dump the contents of a variable into HTML comments for debugging: function debugVar ($var) { global $CFG; if ($CFG->debug) { echo "\n<!--\nDEBUG INFO: \n"; if (is_array ($var)) print_r ($var) else var_dump ($var); echo "\n-->\n"; } } // Dump function parameters into HTML comments for debugging: function debugFunc () { global $CFG; if ($CFG->debug) { $argv = func_get_args (); echo "\n<!--\nDEBUG INFO: \n"; print_r ($argv); echo "\n-->\n"; } } // Dump a debugging message into HTML comments for debugging: function debugMsg ($msg) { global $CFG; if ($CFG->debug) { echo "\n<!--\nDEBUG INFO: \n"; echo $msg; echo "\n-->\n"; } } ?> Usage is simple: To check a variable, call <?php debugVar ($variable); ?> To check parameters passed to a function, call <?php debugFunc ($var1, $var2, $var3 ...); ?> To print a debug message, call <?php debugMsg ("Function xyz returned " . $retVal); ?> Then check your HTML source to find the debugging output in the comments. Don't forget to set $CFG->debug to 0 before rollout. If you're paranoid you can also include a javascript alert() box to be triggered by a : $CFG->debug > 0" condition upon each page reload, although that gets really annoying really fast. :-) // Frank --was-- I always include debugging code in all may major projects. I use an object (e.g. $CFG) to store the settings the control the code's behavior, which includes a $CFG->debug variable to control the level of debugging verbosity. I set it to 0 before rollout, which ensures that even if some debugging code was left in accidentally, this will be deactivated and not spew debugging information in a live website. I use the following functions to facilitate debugging messages. They live in a single code module which I only need include() to use it. Note the global reference to $CFG: // Dump the contents of a variable into HTML comments for debugging: function debugVar ($var) { global $CFG; if ($CFG->debug) { echo "\n<!--\nDEBUG INFO: \n"; if (is_array ($var)) print_r ($var) else var_dump ($var); echo "\n-->\n"; } } // Dump function parameters into HTML comments for debugging: function debugFunc () { global $CFG; if ($CFG->debug) { $argv = func_get_args (); echo "\n<!--\nDEBUG INFO: \n"; print_r ($argv); echo "\n-->\n"; } } // Dump a debugging message into HTML comments for debugging: function debugMsg ($msg) { global $CFG; if ($CFG->debug) { echo "\n<!--\nDEBUG INFO: \n"; echo $msg; echo "\n-->\n"; } } Usage is simple: To check a variable, call debugVar ($variable); To check parameters passed to a function, call debugFunc ($var1, $var2, $var3 ...); To print a debug message, call debugMsg ("Function xyz returned " . $retVal); Then check your HTML source to find the debugging output in the comments. Don't forget to set $CFG->debug to 0 before rollout. If you're paranoid you can also include a javascript alert() box to be triggered by a : $CFG->debug > 0" condition upon each page reload, although that gets really annoying really fast. :-) // Frank http://php.net/manual/en/debugger.php

« previous php.notes (#163076) next »