Home » Php » VS Code php tag autocomplete

VS Code php tag autocomplete

Posted by: admin April 23, 2020 Leave a comment

Questions:

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?

How to&Answers:

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"
}

Answer:

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"
  }
}

Answer:

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

Answer:

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)

Answer:

In Visual Studio Code go to File > Preferences > User Snippets > “html.json” file and paste this code:

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

Answer:

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 ?>"
  }
}