[wp-hackers] quick request for fix idea (print_r and returning)

David Schlosnagle schlosna at gmail.com
Wed Aug 25 16:07:09 UTC 2004


It sounds like you are looking for a function to return the contents
of a variable. From <http://www.php.net/print_r>, here is a function
in the user contributed notes at the bottom that looks like it will
work for you. This isn't my code, so I haven't tested it, but it
should give you a start.

<?php
   function Dump($vardump,$key = false,$level=0,$return=""){       
  
       //make layout
       for ($i = 0;$i<$level;$i++) $return .= "\t";
       if ($level != 0) $key = $key." =>";
      
       //look for objects
       if (is_object($vardump))
           $return .= $key." ".get_class($vardump)." ".$vardump."\n";
       else
           $return .= $key." ".$vardump."\n";
      
       //look recursief
       switch(gettype($vardump)){
           case "object":
               $vars=(array)get_object_vars($vardump);
               foreach($vars as $key => $value)
                   //for in a object
                   //$this->Dump($value,$key,$level+1,&$return);
                   //for normal function you can use
                   Dump($value,$key,$level+1,&$return);
           break;
           case "array":
               foreach ($vardump as $key => $value)
                   //for in a object
                   //$this->Dump($value,$key,$level+1,&$return);
                   //for normal function you can use
                   Dump($value,$key,$level+1,&$return);
           break;
       }
       //return everything
       if ($level == 0) return $return;
   }
?>

On Wed, 25 Aug 2004 15:54:25 +0000, m at tidakada.com <m at tidakada.com> wrote:
> hello,
> 
> I'm currently fixing a new xmlrpc.php for WP 1.3 (or maybe it'll be ready
> later?) and in the meanwhile fixing bugs I find in the present one.
> 
> One of these bugs is related to usage of print_r($somearray, true).
> The second argument, used to avoid print_r from outputting stuff to the UA (and
> thus to avoid invalid XML errors) only works in PHP 4.3.0 and up.
> WP doesn't require PHP 4.3.0 yet, so a solution to this print_r problem is
> needed.
> 
> So here's the request: can anyone find a way to get a pretty print version of an
> array in order to log it when debugging, that works on PHP 4.1.0?
> Thanks in advance.
> 
> --
> Michel
> http://zengun.org/weblog/
> 
> _______________________________________________
> hackers mailing list
> hackers at wordpress.org
> http://wordpress.org/mailman/listinfo/hackers_wordpress.org
> 


-- 


- David Schlosnagle
<schlosna at gmail.com>
<http://www.david-s.net/>



More information about the hackers mailing list