Der findes flere metoder til at debugge et php script.
Men er man på et webhotel er det ikke altid lige nemt at installere nødvendige programmer
.
Med dump.php kan man hurtig se variabler og deres værdi
.
<?php
function dump()
{
$dump_body_background = "navy";
$dump_body_font_color = "yellow";
$dump_body_border = "2px";
$dump_body_border_color = "red";
$dump_body_border_type = "solid";
$dump_body_border_padding = "5px";
$top_legend_background_color = "lightgrey";
$info_legend_background_color = "lightgrey";
$top_legend_padding = "5px";
$show_info_background = "lightgrey";
$show_info_font = "blue";
$show_info_padding = "5px";
$top_legend_font_color = "blue";
$show_info = 1; // 0 = ingen info
// $fil = "File";
// $line = "line";
// $variable_name = "Variable name";
// $variable_type = "Variable type";
// $of = "of";
$fil = "Fil";
$line = "linje";
$variable_name = "Variabel navn";
$variable_type = "Variabel type";
$of = "af";
if(!function_exists('_dump_type'))
{
function _dump_type($value)
{
$type = "";
if(is_int($value)) $type = 'integer';
if(is_null($value)) $type = 'null';
if(is_string($value)) $type = 'string';
if(is_float($value)) $type = 'float';
if(is_bool($value)) $type = 'boolean';
if(is_array($value)) $type = 'array';
if(is_object($value)) $type = 'object';
return $type;
}
}
if(!function_exists('_d_'))
{
function _d_($value,$level=0)
{
if ($level==-1)
{
$trans["\t"]='⇒';
$trans["\n"]='¶;';
$trans["\r"]='↵';
$trans["\0"]='⊕';
return strtr(htmlspecialchars($value),$trans);
}
if ($level==0)
echo '<pre>';
$type = _dump_type($value);
if ($type == 'string')
{
$value= _d_($value,-1);
}
elseif ($type == 'boolean')
{
$value = ($value?'true':'false');
}
elseif ($type == 'object')
{
$props= get_class_vars(get_class($value));
echo $type.' ('.count($props).') <u>'.get_class($value).'</u>';
foreach($props as $key=>$val)
{
echo "\n".str_repeat("\t",$level+1).$key.' => ';
_d_($value->$key,$level+1);
}
$value= '';
}
elseif ($type=='array')
{
echo $type.' ('.count($value).')';
foreach($value as $key=>$val)
{
echo "\n".str_repeat("\t",$level+1)._d_($key,-1).' => ';
_d_($val,$level+1);
}
$value= '';
}
echo '<strong>'.$value.'</strong>';
if ($level==0) echo '</pre>';
}
}
list($debug_list) = debug_backtrace();
$arguments = func_get_args();
$total_arguments = count($arguments);
$source = file($debug_list['file']);
echo '<fieldset style="background:'.$dump_body_background.'; color: '.$dump_body_font_color.'; !important; border:'.$dump_body_border.' '.$dump_body_border_color.' '.$dump_body_border_type.'; padding:'.$dump_body_border_padding.'">';
echo '<legend style="background:'.$top_legend_background_color.'; color:'.$top_legend_font_color.'; padding:'.$top_legend_padding.';">'.$fil.': '.$_SERVER['HTTP_HOST'].$debug_list['file'].' @ '.$line.': '.$debug_list['line'].'</legend><pre>';
if ($show_info >= 1)
{
echo '<fieldset style="background:'.$show_info_background.'; color:'.$show_info_font.'; padding:'.$show_info_padding.';">';
echo '⇒ <b>=> tab (\t)</b> ¶ <b>=> new line (\n)</b> ↵; <b>=> carrige retun (\r)</b> ⊕ <b>=> (\0)</fieldset></b>';
}
$i = 0;
foreach ($arguments as $argument)
{
$tmp = explode("(",$source[$debug_list['line']-1]);
$tmp = explode(")",$tmp[1]);
$var_names = explode(",",$tmp[0]);
$type = _dump_type($argument);
if ($type == 'string')
$type .= ' ('.strlen($argument).')';
echo '<strong>Debug #'.(++$i).' '.$of.' '.$total_arguments.'. '.$variable_name.': '.trim($var_names[$i-1]).' '.$variable_type.': '.$type.'</strong>';
_d_($argument);
}
echo "</pre></fieldset>";
}
?>
Her er et eksempel på brug af dump.php
<?php
session_start();
// Include the dump.php file
include_once "dump.php";
// ############# #
function test()
{
return false;
}
// -------------- -
function test_null()
{
}
// ############# #
echo '<!DOCTYPE HTML><html><head><meta http-equiv="content-type" content="text/html;charset=utf-8" /></head><body>';
// -------------- -
// add a SESSION variable
$_SESSION['my_test'] = "test of dump.php";
echo '<p><fieldset><legend>dump a SESSION variable</legend> <code>dump($_SESSION["my_test");</code></fieldset></p>';
dump($_SESSION["my_test"]);
echo '<p><fieldset><legend>dump all SESSION variable</legend> <code>dump($_SESSION);</code></fieldset></p>';
dump($_SESSION);
// -------------- -
// Test of object
class User
{
public $first_name;
public $last_name;
public function __construct($first, $last)
{
$this->first_name = $first;
$this->last_name = $last;
}
public function __toString()
{
return "User [first='$this->first_name', last='$this->last_name']";
}
}
$user_1 = new User('John', 'Doe');
$user_2 = new User('Jane', 'Doe');
// -------------- -
echo '<p><fieldset><legend>dump object </legend> <code>dump($user_1);</code></fieldset></p>';
dump($user_1);
echo '<p><fieldset><legend>dump object </legend> <code>dump($user_2);</code></fieldset></p>';
dump($user_2);
echo '<p><fieldset><legend>dump 2 variable </legend> <code>dump($user_1, $user_2);</code></fieldset></p>';
dump($user_1, $user_2);
// -------------- -
echo '<p><fieldset><legend>dump a string (string name is also name of variable)</legend> <code>dump("test");</code></fieldset></p>';
dump("test");
echo '<p><fieldset><legend>dump a float</legend><code>dump($pi);</code></fieldset></p>';
$pi = 3.1415926535897;
dump($pi);
echo '<p><fieldset><legend>dump a integer</legend><code>dump($int_);</code></fieldset></p>';
$int_ = 12345;
dump($int_);
// -------------- -
echo '<p><fieldset><legend>dump the returned result from a function </legend><code>dump(file("dump.php"));</code></fieldset></p>';
dump(file("dump.php"));
echo '<p><fieldset><legend>dump the returned result from function </legend><code>dump(test());</code></fieldset></p>';
dump(test());
// -------------- -
echo '<p><fieldset><legend>dump the $_SERVER variable</legend><code>dump($_SERVER);</code></fieldset></p>';
dump($_SERVER);
echo '<p><fieldset><legend>dump a NULL value</legend><code>dump(test_null())</code></fieldset></p>';
dump(test_null());
?>
Indlæg senest redigeret d. 29.01.2011 16:13 af Bruger #16393