Shortcut for echo "<pre>";print_r($myarray);echo "</pre>";
This is the shortest:
echo '<pre>',print_r($arr,1),'</pre>';
The closing tag can also be omitted.
Nope, you'd just have to create your own function:
function printr($data) {
echo "<pre>";
print_r($data);
echo "</pre>";
}
Apparantly, in 2018, people are still coming back to this question. The above would not be my current answer. I'd say: teach your editor to do it for you. I have a whole bunch of debug shortcuts, but my most used is vardd
which expands to: var_dump(__FILE__ . ':' . __LINE__, $VAR$);die();
You can configure this in PHPStorm as a live template.
You can set the second parameter of print_r
to true
to get the output returned rather than directly printed:
$output = print_r($myarray, true);
You can use this to fit everything into one echo
(don’t forget htmlspecialchars
if you want to print it into HTML):
echo "<pre>", htmlspecialchars(print_r($myarray, true)), "</pre>";
If you then put this into a custom function, it is just as easy as using print_r
:
function printr($a) {
echo "<pre>", htmlspecialchars(print_r($a, true)), "</pre>";
}