note 70883 added to function.print-r

From: Date: Thu, 02 Nov 2006 15:56:40 +0000
Subject: note 70883 added to function.print-r
Groups: php.notes 
Request: Send a blank email to php-notes+get-119316@lists.php.net to get a copy of this message
some functions I find useful for debugging. /** * dump the object in a base.class.php :: print_r2() fashon this function is for debugging purposes only (obviously) @see d2() @param $obj an array of elements, that may have elements that are also arrays. @param $color the color of the output (useful, when you need to call this several times, and you need a certain structure to stand out from the others, or simply to differentiate them) color-coding stuff is a great wa to work, consider this: @code p2($struct,'red'); @endcode to find the function call, just search "red". in case you find yourself lost, you can always sue the line numbers, @see $dinfo @param $dinfo see http://www.php.net/debug_backtrace for nifo on debug info., 0: no info, 1: function call, 2: full backtrace. @param $title simple remarks you can put to aid you in development. i.e. @code $a=array(1,2,3,5,6) //this p2($a,'',0,'the numbers'); //is more meaningful than this: p2($a); @code This is particulary true when dealing with many p2() It is recomended that you *don't* delete p2()'s calls from your code, but rather document them: @code //insightfull comments about spected output //p2($a123,'',1,'Items in list') @endcode or, if you feel more confortable with logging, use this: $this->log(gp2($struct)); @see gp2() You can also use p2() as a call trace: helper @code function a(){ b(); } function b(){ c(); } function d(){ a(); } function c(){ p2(1,'',2,'backtrace'); } d(); @endcode note the use of p2(1), as a simple backtrace enables, you can use p2($anything,'',2) to display the log, or p2($anything) to show the call route. the call route follows the following format: @ file : function : function_call_line_number -> current_line_number i.e. @std.php:filter_link():1222->2142 where current line number is the line where the p2() call is, and "function call number" is the line where the current function was called from (useful as hell). -- * */ function p2($obj,$color='black',$dinfo=1,$title=''){ //echo(); $dbg=debug_backtrace(); if($dinfo!=0){ //':'.$dbg[1]['class']. $dinfo="@".basename($dbg[1]['file']).':'.$dbg[1]['function'].'():'. $dbg[1]['line'].'->'.$dbg[0]['line']."\n"; } if($dinfo==2){ $dinfo.=print_r(debug_backtrace(),true); } if($color==''){$color='rgb(50,50,200)';} echo("<h3 style='color:$color'>".$title.'</h3><xmp style="color:'.$color.'">'. $dinfo.print_r($obj,1)."</xmp>"); } function gp2($obj,$color='black'){ return("<xmp style='color:$color'>".print_r($obj,1)."</xmp>"); } /** * @see d2() * */ function d2_recursive($obj,$label='',$color='rgb(100,100,100)'){ if(is_array($obj)){ $dx="\n<table style='border:1px solid ".$color."' cellspacing=0 cellpadding=2 width=100%>"; if($label!=''){ $dx.="\n<tr><td style='background-color:".$color.";border:1px solid ".$color. "' colspan=2>".$label."</td></tr>"; } foreach($obj as $k=>$v){ $dx.="\n<tr><td style='width:1%;color:white;font-weight:bold;font-family:verdana;". "font-size:10pt;background-color:".$color.";border:1px solid ".$color."'>".$k .'</td><td style="border:1px solid '.$color.'">'; $dx.=d2_recursive($v,'',$color)."</td></tr>"; } $dx.="\n</table>"; }else{ $dx=$obj; } return($dx); } /** \brief nice html object inspector * @see p2() * */ function d2($obj,$label='',$color='black'){ echo("\n<!--DUMP:$label START-->\n".d2_recursive($obj,$label,$color)."\n<!--DUMP END-->\n"); } ---- Server IP: 66.163.161.117 Probable Submitter: 200.71.48.222 ---- Manual Page -- http://www.php.net/manual/en/function.print-r.php Edit -- https://master.php.net/note/edit/70883 Del: integrated -- https://master.php.net/note/delete/70883/integrated Del: useless -- https://master.php.net/note/delete/70883/useless Del: bad code -- https://master.php.net/note/delete/70883/bad+code Del: spam -- https://master.php.net/note/delete/70883/spam Del: non-english -- https://master.php.net/note/delete/70883/non-english Del: in docs -- https://master.php.net/note/delete/70883/in+docs Del: other reasons-- https://master.php.net/note/delete/70883 Reject -- https://master.php.net/note/reject/70883 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#119316) next »