when viewing arrays ... any ideas? I often have to browse data in arrays and use the h...">

How to avoid typing <pre class = "prettyprint-override"> when viewing arrays ... any ideas?

I often have to browse data in arrays and use the html tag <pre>for this. However, it becomes tedious and disappointing to introduce the same thing every time.

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

+3
source share
8 answers

It's funny that you should ask about it, I just wrote a short function to save me from the pain associated with this.

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;
}    

, . , . , (, 1)

:.

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

pre(); //just prints <pre>
print "hello";
pre(1); //just prints </pre>
-1

, <pre>. :

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

print_r pretty_print. <pre> :)

+12

XDebug. , print_r var_dump ( ), .

+4
function pre_($array)
{
  echo '<pre>' . print_r( $array, true ) . '</pre>';
}
+2

- . , .

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 want.

+1
source

I use this

function d($obj)
{
   ob_start();
   print_r($obj);
   $output = htmlspecialchars(ob_get_clean());
   echo "<pre>$output</pre>";
}
+1
source

I am changing the default_mimetype(default text/html) php.ini parameter to text/plain.

0
source

One of my favorite tricks if printing an array is all I do:

header('Content-type: text/plain');
print_r($arr);
0
source

Source: https://habr.com/ru/post/1758898/


All Articles