多半函数都是来自于PHP.NET,对其进行了修改和调整。
命名为variable_inspector.php。
源码:
<?php
/*
* Created on 2011/1/6
* by Vincent
* Collection of functions that work for inspection of variables
* From PHP.NET
* http://php.net/manual/en/function.var-dump.php
*/
//Work well for HTML envirement:
//cant be used to display object
function variable_to_html($variable) {
if ($variable === true) {
return 'true';
} else if ($variable === false) {
return 'false';
} else if ($variable === null) {
return 'null';
} else if (is_array($variable)) {
$html = "<br><table border=/"1/">/n";
$html .= "<thead><tr><td><b>KEY</b></td><td><b>VALUE</b></td></tr></thead>/n";
$html .= "<tbody>/n";
foreach ($variable as $key => $value) {
$value = variable_to_html($value);
$html .= "<tr><td>$key</td><td>$value</td></tr>/n";
}
$html .= "</tbody>/n";
$html .= "</table><br>";
return $html;
} else {
return strval($variable);
}
}
// Function: dump_highstrike
// Inspired from: PHP.net Contributions
// Description: Helps with php debugging
function dump_highstrike(&$var, $info = FALSE)
{
$scope = false;
$prefix = 'unique';
$suffix = 'value';
if($scope) $vals = $scope;
else $vals = $GLOBALS;
$old = $var;
$var = $new = $prefix.rand().$suffix; $vname = FALSE;
foreach($vals as $key => $val) if($val === $new) $vname = $key;
$var = $old;
echo "<pre style='margin: 0px 0px 10px 0px; display: block; background: white; color: black; font-family: Verdana; border: 1px solid #cccccc; padding: 5px; font-size: 10px; line-height: 13px;'>";
if($info != FALSE) echo "<b style='color: red;'>$info:</b><br>";
do_dump($var, '$'.$vname);
echo "</pre>";
}
// Function: do_dump
// Inspired from: PHP.net Contributions
// Description: Better GI than print_r or var_dump
function do_dump(&$var, $var_name = NULL, $indent = NULL, $reference = NULL)
{
$do_dump_indent = "<span style='color:#eeeeee;'>|</span> ";
$reference = $reference.$var_name;
$keyvar = 'the_do_dump_recursion_protection_scheme'; $keyname = 'referenced_object_name';
if (is_array($var) && isset($var[$keyvar]))
{
$real_var = &$var[$keyvar];
$real_name = &$var[$keyname];
$type = ucfirst(gettype($real_var));
echo "$indent$var_name <span style='color:#a2a2a2'>$type</span> = <span style='color:#e87800;'>&$real_name</span><br>";
}
else
{
$var = array($keyvar => $var, $keyname => $reference);
$avar = &$var[$keyvar];
$type = ucfirst(gettype($avar));
if($type == "String") $type_color = "<span style='color:green'>";
elseif($type == "Integer") $type_color = "<span style='color:red'>";
elseif($type == "Double"){ $type_color = "<span style='color:#0099c5'>"; $type = "Float"; }
elseif($type == "Boolean") $type_color = "<span style='color:#92008d'>";
elseif($type == "NULL") $type_color = "<span style='color:black'>";
if(is_array($avar))
{
$count = count($avar);
echo "$indent" . ($var_name ? "$var_name => ":"") . "<span style='color:#a2a2a2'>$type ($count)</span><br>$indent(<br>";
$keys = array_keys($avar);
foreach($keys as $name)
{
$value = &$avar[$name];
do_dump($value, "['$name']", $indent.$do_dump_indent, $reference);
}
echo "$indent)<br>";
}
elseif(is_object($avar))
{
echo "$indent$var_name <span style='color:#a2a2a2'>$type</span><br>$indent(<br>";
foreach($avar as $name=>$value) do_dump($value, "$name", $indent.$do_dump_indent, $reference);
echo "$indent)<br>";
}
elseif(is_int($avar)) echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> $type_color$avar</span><br>";
elseif(is_string($avar)) echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> $type_color/"$avar/"</span><br>";
elseif(is_float($avar)) echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> $type_color$avar</span><br>";
elseif(is_bool($avar)) echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> $type_color".($avar == 1 ? "TRUE":"FALSE")."</span><br>";
elseif(is_null($avar)) echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> {$type_color}NULL</span><br>";
else echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> $avar<br>";
$var = $var[$keyvar];
}
}
// An elegant dump
// By BigueNique@yahoo.ca
$elegant_dump_indent = '|  ';
function elegant_dump(&$var, $var_name='', $indent='', $reference='') {
global $elegant_dump_indent;
$reference=$reference.$var_name;
// first check if the variable has already been parsed
$keyvar = 'the_elegant_dump_recursion_protection_scheme';
$keyname = 'referenced_object_name';
if (is_array($var) && isset($var[$keyvar])) {
// the passed variable is already being parsed!
$real_var=&$var[$keyvar];
$real_name=&$var[$keyname];
$type=gettype($real_var);
echo "$indent<b>$var_name</b> (<i>$type</i>) = <font color=/"red/">&$real_name</font><br>";
} else {
// we will insert an elegant parser-stopper
$var=array($keyvar=>$var,
$keyname=>$reference);
$avar=&$var[$keyvar];
// do the display
$type=gettype($avar);
// array?
if (is_array($avar)) {
$count=count($avar);
echo "$indent<b>$var_name</b> (<i>$type($count)</i>) {<br>";
$keys=array_keys($avar);
foreach($keys as $name) {
$value=&$avar[$name];
elegant_dump($value, "['$name']", $indent.$elegant_dump_indent, $reference);
}
echo "$indent}<br>";
} else
// object?
if (is_object($avar)) {
echo "$indent<b>$var_name</b> (<i>$type</i>) {<br>";
foreach($avar as $name=>$value) elegant_dump($value, "->$name", $indent.$elegant_dump_indent, $reference);
echo "$indent}<br>";
} else
// string?
if (is_string($avar)) echo "$indent<b>$var_name</b> (<i>$type</i>) = /"$avar/"<br>";
// any other?
else echo "$indent<b>$var_name</b> (<i>$type</i>) = $avar<br>";
$var=$var[$keyvar];
}
}
//Work well for plain text envirement, better for log:
function var_log(&$varInput, $var_name='', $reference='', $method = '=', $sub = false) {
static $output ;
static $depth ;
if ( $sub == false ) {
$output = '' ;
$depth = 0 ;
$reference = $var_name ;
$var = serialize( $varInput ) ;
$var = unserialize( $var ) ;
} else {
++$depth ;
$var =& $varInput ;
}
// constants
$nl = "/n" ;
$block = 'a_big_recursion_protection_block';
$c = $depth ;
$indent = '' ;
while( $c -- > 0 ) {
$indent .= '| ' ;
}
// if this has been parsed before
if ( is_array($var) && isset($var[$block])) {
$real =& $var[ $block ] ;
$name =& $var[ 'name' ] ;
$type = gettype( $real ) ;
$output .= $indent.$var_name.' '.$method.'& '.($type=='array'?'Array':get_class($real)).' '.$name.$nl;
// havent parsed this before
} else {
// insert recursion blocker
$var = Array( $block => $var, 'name' => $reference );
$theVar =& $var[ $block ] ;
// print it out
$type = gettype( $theVar ) ;
switch( $type ) {
case 'array' :
$output .= $indent . $var_name . ' '.$method.' Array ('.$nl;
$keys=array_keys($theVar);
foreach($keys as $name) {
$value=&$theVar[$name];
var_log($value, $name, $reference.'["'.$name.'"]', '=', true);
}
$output .= $indent.')'.$nl;
break ;
case 'object' :
$output .= $indent.$var_name.' = '.get_class($theVar).' {'.$nl;
foreach($theVar as $name=>$value) {
var_log($value, $name, $reference.'->'.$name, '->', true);
}
$output .= $indent.'}'.$nl;
break ;
case 'string' :
$output .= $indent . $var_name . ' '.$method.' "'.$theVar.'"'.$nl;
break ;
default :
$output .= $indent . $var_name . ' '.$method.' ('.$type.') '.$theVar.$nl;
break ;
}
// $var=$var[$block];
}
-- $depth ;
if( $sub == false )
return $output ;
}
//for primary variables
function print_primary_var(&$var, $scope=false, $prefix='unique', $suffix='value')
{
//echo $var;
$html = "<br><table border=/"1/">/n";
$html .= "<thead><tr><td><b>NAME</b></td><td><b>TYPE</b></td><td><b>VALUE</b></td></tr></thead>/n";
$html .= "<tbody>/n";
$html .= "<tr><td>";
$html .= vname(&$var, $scope=false, $prefix='unique', $suffix='value');
if (is_bool($var)) {
$type = "BOOL";
if ($var === true)
{
$valStr = 'true';
}
else if ($var === false)
{
$valStr = 'false';
}
} else if (is_int($var)) {
$type = "INT";
$valStr = $var;
} else if (is_float($var)) {
$type = "FLOAT";
$valStr = $var;
} else if (is_string($var)) {
$type = "STRING";
$valStr = $var;
} else if (is_null($var)) {
$type = "NULL";
$valStr = "NULL";
} else if (is_array($var)) {
$type = "ARRAY";
$valStr = "Dont call me please. Use inspectVars instead.";
} else if (is_object($var)) {
$type = "OBJECT";
$valStr = "Dont call me please. Use inspectVars instead.";
} else {
$type = "UNKNOWN";
$valStr = $var;
}
$html .= "</td><td>$type";
$html .= "</td><td>$valStr</td></tr>/n";
$html .= "</tbody>/n";
$html .= "</table><br>";
return $html;
}
function vname(&$var, $scope=false, $prefix='unique', $suffix='value')
{
if($scope)
$vals = $scope;
else
$vals = $GLOBALS;
$old = $var;
$var = $new = $prefix.rand().$suffix;
$vname = FALSE;
foreach($vals as $key => $val)
{
if($val === $new) $vname = $key;
}
$var = $old;
return $vname;
}
?>
![variable_to_html](http://hi.csdn.net/attachment/201101/6/2190190_12942999886R6V.png)
![dump_highstrike](http://hi.csdn.net/attachment/201101/6/2190190_1294299964zz3X.png)
![elegant_dump](http://hi.csdn.net/attachment/201101/6/2190190_129430363194X1.png)