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

Viewed 180133

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.

19 Answers

If you use VS CODE, you can use : Ctrl + Shift + P -> Configure User Snippets -> PHP -> Enter

After that you can input code to file php.json :

"Show variable user want to see": {
  "prefix": "pre_",
  "body": [
     "echo '<pre>';",
     "print_r($variable);",
     "echo '</pre>';"
  ],
  "description": "Show variable user want to see"
}

After that you save file php.json, then you return to the first file with any extension .php and input pre_ -> Enter Done, I hope it helps.

Just write

print_r($myarray); //it will display you content of an array $myarray

exit(); //it will not execute further codes after displaying your array

I generally like to create my own function as has been stated above. However I like to add a few things to it so that if I accidentally leave in debugging code I can quickly find it in the code base. Maybe this will help someone else out.

function _pr($d) {
    echo "<div style='border: 1px solid#ccc; padding: 10px;'>";
    echo '<strong>' . debug_backtrace()[0]['file'] . ' ' . debug_backtrace()[0]['line'] . '</strong>';
    echo "</div>";
    echo '<pre>';
    if(is_array($d)) {
    print_r($d);
    } else if(is_object($d)) {
    var_dump($d);
    }
    echo '</pre>';
}

You can create Shortcut key in Sublime Text Editor using Preferences -> Key Bindings

Now add below code on right-side of Key Bindings within square bracket []

{ 
    "keys": ["ctrl+shift+c"],
    "command": "insert_snippet",
    "args": { "contents": "echo \"<pre>\";\nprint_r(${0:\\$variable_to_debug});\necho \"</pre>\";\ndie();\n" }
}

Enjoy your ctrl+shift+c shortcut as a Pretty Print of PHP.

For more simpler way

echo ""; print_r($test); exit();

Related