readability of arrays with print_r

Discussion in 'PHP' started by Greenmethod, Oct 16, 2007.

  1. #1
    Is there a way that you can make the browser output the array like it does on php.net? When I output the array, it prints it in a long line, and I would like it to output it with a line break in between the rows. Thanks for the help in advance!
     
    Greenmethod, Oct 16, 2007 IP
  2. bubbles19518

    bubbles19518 Peon

    Messages:
    73
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <?
    for($i=0;i<count($array);$i++)
    {
    echo($array[$i]."\n");
    }
    ?>
    
    Code (markup):
     
    bubbles19518, Oct 16, 2007 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    Try this:

    
    echo "<pre>";
    print_r($array);
    echo "</pre>";
    
    PHP:
    You can easily make a function or a method in a class for easy debugging as well.
     
    jestep, Oct 16, 2007 IP
  4. tamen

    tamen Peon

    Messages:
    182
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Exactly. I always have this function in my functions.php when developing:
    
    	function pp($p)
    	{
    		echo is_array($p) ? '<pre>'."\n" : '';
    		print_r($p);
    		echo is_array($p) ? "\n".'</pre>' : '';
    		unset($p);
    	}
    
    Code (markup):
    You can use it both for regular vars and arrays. Regular vars will not get surroundedn by pre-tags, arrays will.
     
    tamen, Oct 17, 2007 IP