51

kind of trivial question but here it goes. I can't figure out why VS Code autocompletes (with tab) all html tags correctly while php tag not. When i type "php" and hit tab, it creates this:

<php></php>

which is useless and i have no idea why it's there. I want it to do normal php tag

<?php ?>

I have turned off suggestions as they were distracting me. Anyway to edit default/top "tab autocomplete" item?

Mezotar
  • 511
  • 1
  • 4
  • 5

9 Answers9

130

this hack worked for me: go to 'Preferences: Open User Snippets', and paste this snippet into the html (yes, that's html) user snippets:

"php": {
    "prefix": "php",
    "body": [
        "<?php $1 ?>"
    ],
    "description": "php tag"
}
Lane
  • 6,532
  • 5
  • 28
  • 27
  • To be fair i don't know why isn't this a built-in snippet. How likely is it that you would want to add a `` tag ? May one could suggest this to the dev team ? – Isac Apr 07 '18 at 08:21
  • 1
    Does this work inside quotation? Doesn't work in my case... any workaround? – norixxx Oct 16 '18 at 05:57
  • Thanks a lot for the snippet. Don't know why VS Code doesn't ship with it built-in though. – iSaumya Jun 12 '19 at 07:16
  • 2
    Worked for me. Thanks! (Had the same problem as @norixxx. Fixed as per [here](https://stackoverflow.com/a/58009077/499167), with a little help from [here](https://supunkavinda.blog/vscode-editing-settings-json)) – 681234 Mar 31 '20 at 17:50
  • Instead of 'Preferences: Open User Snippets', I had to type 'Preferences: Configure User Snippets' to configure the snippets. And then it worked. Thanks a lot. – Sandesh Yadav Apr 20 '22 at 06:52
  • Using $1 as placeholder didn't work for me because I couldn't use any snippets inside php tag itself. Changing this to $0 did the trick. – Daniel Jul 17 '22 at 15:41
13

I would elaborate on Lane's answer by putting the final cursor represented by $0 in a new line between the tags. In the snippet, each comma inside the "body" array represents a new line.

So in Visual Sutiio Code go to File > Preferences > User Snippets search for "html.json" and edit it by pasting this:

{
  // Place your snippets for html here. 
  "php": {
    "prefix": "php",
    "body": ["<?php", "$0", "?>"],
    "description": "php tag"
  }
}
Raphael Pinel
  • 2,352
  • 24
  • 26
6

In Visual Studio Code, go to File > Preferences > User Snippets > html.json file and then paste the following code:

"php": {
    "prefix": "php",
    "body": [
        "<?php $1 ; ?>",
        "$2"
    ],
    "description": "php tag"
}
xKobalt
  • 1,498
  • 2
  • 13
  • 19
Mak Alamin
  • 144
  • 2
  • 7
5

Under File > Preferences there is User Snippets, use html for adding php snippets.

{
  "Snippet name": {
  "prefix": ["keywords", "for", "finding"],
  "body": [
    "<?php ${1:placeholder} ?>"
  ],
  "description": "A php tag."
  }
}

$1, $2 etc. are variables that will be on focus when snippet is entered. You can also use placeholder as presented in my example.

For multiline body, end current line with comma and add content at new line in " ". You can find more information about creating own snippets in VS Code here:

https://code.visualstudio.com/docs/editor/userdefinedsnippets

Jakub Karki
  • 73
  • 1
  • 2
  • 6
3

go in File > Preferences > User Snippets now write in bar php and press enter .

vs code generate file called php.code-snippets , now here paste this :

  { 

    // Place your GLOBAL snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and 
    // description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
    // is left empty or omitted, the snippet gets applied to all languages. The prefix is what is 
    // used to trigger the snippet and the body will be expanded and inserted. Possible variables are: 
    // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. 
    // Placeholders with the same ids are connected.
    // Example:
    "php": {    
        "prefix": "php",
        "body": [
            "<?php //put here code ?>"
        ],
        "description": "php tag"
    } 
}
salvo720
  • 166
  • 1
  • 4
2

As I understand from your question you haven't added the external PHP IntelliSense extension and using the default PHP IntelliSense provided by the VS Code, If that's the situation you need to add the extension to your Visual Studio Code Editor.

To add the extension use keyboard and press Ctrl+Shift+p you will get the command palette and type the following command Extensions: Install Extension then on to the left panel of the editor you will get the option to search for extensions, you may search for PHP IntelliSense and install it. Editor will ask to restart the editor to apply the extension.

I recommend to disable VS Code's built-in PHP IntelliSense by adding the following property "php.suggest.basic":false to the settings json config of the editor (keyboard press Ctrl+, you will get the settings json to the right of the editor) to avoid duplicate suggestions.

For PHP IntelliSense to work You need to have atleast PHP 7 installed, You can either add it to your PATH or set the "php.executablePath":"php physical path" in the settings json (keyboard press Ctrl+, you will get the settings json)

Keshan Nageswaran
  • 8,060
  • 3
  • 28
  • 45
  • 2
    Thank you for you time, however i do have the PHP IntelliSense ext. (including PHP7) installed and even after following your advice and disabling the built-in one, which i hoped would do the trick, it does the same thing. – Mezotar May 01 '17 at 11:58
  • 4
    I haven't met many people who are satisfied using VS Code for PHP. – Ben Coffin Jul 10 '17 at 19:56
0

Very helpful question and answers, thank you all. I also missed the ability to automatically close and reopen php tags from within a php tag, in VS code, so I tried putting a similar snippet in the php.json file and... it works as expected! It's not strictly an answer to the original question, but it's still closely related.

    "php": {
    "prefix": "php",
    "body": [
            "?>$0<?php"
    ],
    "description": "php tag"
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
-2

I prefer a custom keyboard shortcut added to keybindings.json - this lets you add php tags in places where intellisense doesn't work properly, like inside the quotes of html attributes. You can also select some text (maybe you copied some php from elsewhere without the full php tags) and automatically wrap it in php tags. Here is what I use:

{
  "key": "cmd+alt+ctrl+p",
  "command": "editor.action.insertSnippet",
  "when": "editorTextFocus",
  "args": {
    "snippet": "<?php $1$TM_SELECTED_TEXT$0 ?>"
  }
}
NicO
  • 67
  • 1
  • 2
-2

maybe you should install PHP Awesome Snippetts extension.