I usually use this simple function in combo with a die(); in order to have on screen the value of a variable or array:
<?php
function debug_view ( $what ) {
echo '<pre>';
if ( is_array( $what ) ) {
print_r ( $what );
} else {
var_dump ( $what );
}
echo '</pre>';
}
?>
Debugging in PHP
turaz dot w4l at gmail dot com
06-Aug-2009 09:14
06-Aug-2009 09:14
tomboul
09-Apr-2009 08:51
09-Apr-2009 08:51
Here a best printed var_dump : show_php
It's a reccursive function for output many kind of structured data (array, class, etc ...)
<?php
function show_php($var,$indent=' ',$niv='0')
{
$str='';
if(is_array($var)) {
$str.= "<b>[array][".count($var)."]</b><br />";
foreach($var as $k=>$v) {
for($i=0;$i<$niv;$i++) $str.= $indent;
$str.= "$indent<em>\"{$k}\"=></em>";
$str.=show_php($v,$indent,$niv+1);
}
}
else if(is_object($var)) {
$str.= "<b>[objet]-class=[".get_class($var)."]-method=[";
$arr = get_class_methods($var);
foreach ($arr as $method) {
$str .= "[function $method()]";
}
$str.="]-";
$str.="</b>";
$str.=show_php(get_object_vars($var),$indent,$niv+1);
}
else {
$str.= "<em>[".gettype($var)."]</em>=[{$var}]<br />";
}
return($str);
}
?>
EXAMPLE :
array example to debug :
<?php
$tab=array(
"first"=>"firstValue",
"second"=>array(
"first"=>1,
"second"=>2,
"third"=>array(
"first"=>"one",
"second"=>"two"
),
),
);
?>
result of show_php :
<?php
echo "tab=".show_php($tab);
?>
tab=[array][2]
"first"=>[string]=[firstValue]
"second"=>[array][3]
"first"=>[integer]=[1]
"second"=>[integer]=[2]
"third"=>[array][2]
"first"=>[string]=[one]
"second"=>[string]=[two]
result of var_dump :
<?php
echo "tab=";
var_dump($tab);
echo "<br>";
?>
tab=array(2) { ["first"]=> string(10) "firstValue" ["second"]=> array(3) { ["first"]=> int(1) ["second"]=> int(2) ["third"]=> array(2) { ["first"]=> string(3) "one" ["second"]=> string(3) "two" } } }
Diego
18-Feb-2009 10:21
18-Feb-2009 10:21
If you don't find a syntax error, you can comment out a block where you assume the error (or put it out of the document by [ctrl] + [X], but keep a copy on your HD for the case, your computer crashes) and check, if the syntax error is still there.
If not, it must be anywhere in your commented text; if yes, it must be somewhere else.
If you want to locate the error better, do it again with an other and/or smaller piece of code, till you get it.
online _ use _ only == hotmail.com
15-Mar-2006 09:41
15-Mar-2006 09:41
I still find that printing out variable values at problem points in the code is one of the easiest ways for me to debug. If you're interested in knowing the full contents of an object/array/scalar, then use
var_dump($var).
