Histórico de 11 de diciembre de 2009

[PHP] debug() function

Función utilizada para mostrar el valor de una variable. Normalmente la uso para depurar el código.

/**
 * debug; Prints a string and a variable value in red (for debugging purposes).
 * @name string containing the name of the variable
 */
function debug($name) {
	global ${$name};
	global $debug;
	$the_style = "color:#f00;font-weight:bold;";
	// Only show output if debugging is on
	if ($debug == 1) {
		$var = ${$name};
		if (is_array($var)) {
			echo "##<b class=\"bold\">$name</b><br>\n";
			while (list($key, $value) = each($var)){
				print("\n".'<div style="'.$the_style.'">#=>'.$key.' ==> ');
				if (is_array($value)){
					print('<code>');
					print_r($value);
					print('</code>');
				} else {
					echo nl2br($value);
				}
				print('</div>'."\n");
			}
		} else {
			print('<div style="'.$the_style.'">#'.$name.'=>'.$var.'#</div>'."\n");
		}
	}
}

Ejemplo de uso:

<?php
$debug = 1;
// ...
$mi_mensaje = "Mola Hundo!";
debug("mi_mensaje"); // OJO, se pone el _nombre_ de la variable, no la variable
// ...
?>