Guest User

Better then var_dump

a guest
Oct 26th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.10 KB | None | 0 0
  1. /*
  2.  Will output a nice table into your browser showing the contents of your array or element.
  3.  Handy when debugging.
  4.  
  5.  call like this `echo print_nice($arr, 15);`
  6.   15 means maximum depth
  7. */
  8.  
  9. function print_nice($elem,$max_level=15,$print_nice_stack=array()){
  10.     if(is_array($elem) || is_object($elem)){
  11.         if(in_array($elem,$print_nice_stack,true)){
  12.             echo "<font color=red>RECURSION</font>";
  13.             return;
  14.         }
  15.         $print_nice_stack[]=&$elem;
  16.         if($max_level<1){
  17.             echo "<font color=red>nivel maximo alcanzado</font>";
  18.             return;
  19.         }
  20.         $max_level--;
  21.         echo "<table border=1 cellspacing=0 cellpadding=3 width=100%>";
  22.         if(is_array($elem)){
  23.             echo '<tr><td colspan=2 style="background-color:#333333;"><strong><font color=white>ARRAY</font></strong></td></tr>';
  24.         }else{
  25.             echo '<tr><td colspan=2 style="background-color:#333333;"><strong>';
  26.             echo '<font color=white>OBJECT Type: '.get_class($elem).'</font></strong></td></tr>';
  27.         }
  28.         $color=0;
  29.         foreach((array)$elem as $k => $v){
  30.             if($max_level%2){
  31.                 $rgb=($color++%2)?"#888888":"#BBBBBB";
  32.             }else{
  33.                 $rgb=($color++%2)?"#8888BB":"#BBBBFF";
  34.             }
  35.             echo '<tr><td valign="top" style="width:40px;background-color:'.$rgb.';">';
  36.             echo '<strong>'.$k."</strong></td><td>";
  37.             print_nice($v,$max_level,$print_nice_stack);
  38.             echo "</td></tr>";
  39.         }
  40.         echo "</table>";
  41.         return;
  42.     }
  43.     if($elem === null){
  44.         echo "<font color=green>NULL</font>";
  45.     }elseif($elem === 0){
  46.         echo "0";
  47.     }elseif($elem === true){
  48.         echo "<font color=green>TRUE</font>";
  49.     }elseif($elem === false){
  50.         echo "<font color=green>FALSE</font>";
  51.     }elseif($elem === ""){
  52.         echo "<font color=green>EMPTY STRING</font>";
  53.     }else{
  54.         try {
  55.             echo str_replace("\n","<strong><font color=red>*</font></strong><br>\n",$elem);
  56.         } catch (Exception $e) {
  57.            
  58.         }
  59.        
  60.     }
  61. }
Add Comment
Please, Sign In to add comment