tags:

views:

179

answers:

8

I often have the need to view data in arrays and use the html tag <pre> to do this. However, it becomes tedious and frustrating having to type the same thing out all the time.

My question, what is your technique for avoiding this annoyance of programming with PHP?

+10  A: 

There is no way to view it nicely formatted format except for using <pre> tag. Alternatively you can create this function and use that instead:

function pretty_print(array $array){
  echo '<pre>';
  print_r($array);
  echo '</pre>';
}

Now instead of print_r, you can use the pretty_print. No need to type <pre> every now and then :)

Sarfraz
I would copy the CakePHP way and call it `pr()` as then you aren't typing awkward characters.
DavidYell
+4  A: 

You could try something like this. Note it is untested.

function html_var_dump($obj)
{
   ob_start();
   var_dump($obj);
   $output = htmlentities(ob_get_contents());
   ob_end_clean();
   echo "<pre>$output</pre>";
}

You can use print_r instead of var_dump if you prefer.

Brandon Horsley
A: 

Funny you should ask that, I just wrote a short function to save me the pain of having to do this so much.

function pre($option = "open"){                   
  if (is_object($option) || is_array($option)):
    print "<pre>";
    print_r($option);
    print "</pre>";
  else:
    $option=="open"?print "<pre>": print "</pre>";
  endif;
}    

If you pass an array or an object to it, it will print it inside pre tags. If you just want an opening tag, then do not pass an argument. If you want a closing tag, pass it any other argument (e.g. 1)

e.g.:

pre($result); //prints in pre tags

pre(); //just prints <pre>
print "hello";
pre(1); //just prints </pre>
jsims281
I like this as it gives you the option of just outputting the pre tags, or dumping the whole array within pre tags. Multi functional!
Tisch
see this question too: http://stackoverflow.com/questions/2141585/a-more-pretty-informative-var-dump-alternative-in-php
Raveren
great response, cheers guys!
Tisch
@Tisch what's so good in that ambiguousness? why not to just print `<pre>` itself if needed?
Col. Shrapnel
because 'pre();' is less typing than print '<pre>';simples.
Tisch
what if one of your array values is or contains "</pre>"? what if you think you are passing it an array but accidentally pass it a string?
@evilclown - then there will be two </pre> tags in the end result. It's only for debugging purposes so I can get data on the screen in a pre tag with minimal keystrokes. If it's a string it will print a close pre tag :- because I don't usually need to print strings in pre tags for debugging then this isn't really considered here.
jsims281
+4  A: 

Install XDebug. Besides making print_r and var_dump a lot prettier (and more useful), it also has other very handy features.

Maerlyn
+1  A: 

I use this

function d($obj)
{
   ob_start();
   print_r($obj);
   $output = htmlspecialchars(ob_get_clean());
   echo "<pre>$output</pre>";
}
Bang Dao
time to learn highlight_code() function...
Col. Shrapnel
-1 for using output buffering instead of setting the second param of `print_r` to true, which would return the results instead of printing them...
ircmaxell
A: 
function pre_($array)
{
  echo '<pre>' . print_r( $array, true ) . '</pre>';
}
ovais.tariq
i dont think there should be a -1 for a typo
ovais.tariq
A: 

I change the default_mimetype (default text/html) php.ini setting to text/plain.

salathe
A: 

One of my favorite tricks, if printing the array is all I'm doing:

header('Content-type: text/plain');
print_r($arr);
Ryan Kinal