Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortcut for echo "<pre>";print_r($myarray);echo "</pre>";

Tags:

php

Is there a Shortcut for

echo "<pre>";    print_r($myarray); echo "</pre>"; 

It is really annoying typing those just to get a readable format of an array.

like image 622
mrN Avatar asked Sep 24 '10 08:09

mrN


People also ask

What is use of Print_r () function?

The print_r() function prints the information about a variable in a more human-readable way.

What is Echo Pre in PHP?

echo "<pre>"; then you will enable the viewing of multiple spaces and line endings. Without this, all \n , \r and other end line characters wouldn't have any effect in the browser and wherever you had more than 1 space in the code, the output would be shortened to only 1 space.

How Echo () is differ than print () in PHP?

echo and print are more or less the same. They are both used to output data to the screen. The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters (although such usage is rare) while print can take one argument.


2 Answers

This is the shortest:

echo '<pre>',print_r($arr,1),'</pre>'; 

The closing tag can also be omitted.

like image 65
Casey Chu Avatar answered Sep 22 '22 22:09

Casey Chu


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.

like image 29
Dennis Haarbrink Avatar answered Sep 20 '22 22:09

Dennis Haarbrink