'PHP block shortcut in Visual Studio Code

How can I open a basic PHP block in Visual Studio Code, like so - <?php ?>?

In Sublime Text I simply type php and press Tab, and the block is created. But I can't find the same shortcut for Visual Studio Code.



Solution 1:[1]

You can add a custom user snippet to Visual Studio Code:

  • Open "Preferences"
  • User Snippets
  • Select html.json (HTML)
  • Paste the following custom snippet:

    "php": {
        "prefix": "php",
        "body": [ "<?php $1 ?>" ],
        "description": "php tag"
    }
    

Solution 2:[2]

Use:

  1. Open Visual Studio Code

  2. Open menu File ? Preferences ? User Snippets

  3. Search and select "html.json"

  4. Now paste this code in {}:

     "php": {
         "prefix": ["<?", "<? ", "php"],
         "body": [ "<?php $1 ?>" ],
         "description": "php tag"
     }
    

Now you can use "<?" or "<? " or "php".

Solution 3:[3]

I have used this in order to quickly order the space.

    "php": {
    "prefix": ["<?", "php"],
    "body": [
        "<?php",
        "",
        "$0",
        "",
        "?>"
    ],
    "description": "php tag"
}

You get something like this:

        <?php
        
        (code here)
        
        ?>

Solution 4:[4]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Álvaro González
Solution 2 Peter Mortensen
Solution 3 Reichlich
Solution 4 Peter Mortensen