26

I am using Visual Studio Code on a Mac to work on WordPress applications.

Is there a way to make Visual Studio Code recognize HTML and use it's features (mainly autocompletion) inside PHP files?

I've searched for this answer before but just get told to Go to Visual Studio Code Settings: File >> Preferences >> User Settings

// Place your settings in this file to overwrite the default settings
{                
// Configure file associations to languages (e.g. "*.extension": "html"). These have precedence over the default associations of the languages installed.
     "files.associations": {"*.php": "html"}     
 }

But doing this makes you lose all PHP support and autocompletion.

I wish Microsoft would realize that PHP developers commonly use PHP and HTML together when creating applications/websites. I would love to use VS Code instead of the magnificent but expensive PHP storm if possible.

Gama11
  • 31,714
  • 9
  • 78
  • 100
thatemployee
  • 473
  • 1
  • 6
  • 12
  • I can remember I used a extension once, I am not sure if the intellisense worked well. Check this post: https://stackoverflow.com/questions/10529214/how-to-use-php-with-visual-studio – Stefan Aug 29 '17 at 20:42
  • @Stefan thank you for the link but it seems as if these are paid which would defeat the purpose of using a open source editor for me, and I might as well just stick to a better option such as PHPStorm. – thatemployee Aug 29 '17 at 20:45
  • Ah, yes, I didn't read very well. It is worth the effort to do a bit of goolging for them. There is a free one around somewhere. Too bad I can't remember the name :/ – Stefan Aug 29 '17 at 20:48
  • Somehow I don't feel PHP is high on Microsoft's list of priorities. You know, since PHP is a competitor for ASP.NET etc. in that space... – Jonnix Aug 29 '17 at 20:50
  • 2
    "PHP developers commonly use PHP and HTML together" I would actually advise against that whenever possible. – Patrick Q Aug 29 '17 at 20:53
  • 1
    @PatrickQ Yeah because it's totally possible to not use HTML inside template.php files. Common now. – thatemployee Aug 29 '17 at 20:54
  • 2
    @JonStirling Then maybe Microsoft shouldn't try and claim they have 'Fantastic PHP support' – thatemployee Aug 29 '17 at 20:55
  • Define support. Let's face it, all you need to build a PHP web app is the PHP CLI and a text editor and hey presto, you're supported. Add syntax highlighting and suddenly it's fantastic support :P – Jonnix Aug 29 '17 at 20:57
  • Such as HTML autocompletion inside PHP files. Yes, syntax highlighting is the whole purpose of this question. Did you not even read it? @JonStirling – thatemployee Aug 29 '17 at 20:59
  • I can't tell you the last time I had raw HTML in a `.php` file (aside from small, quoted, snippets). It most certainly is possible. If you're creating templates, consider naming them something like `.tpl` or just use a templating system like twig or smarty. – Patrick Q Aug 29 '17 at 21:00
  • That's awesome if you are using your own projects. But I, and 99% of existing code bases for client work in agencies do not. @PatrickQ – thatemployee Aug 29 '17 at 21:01
  • 2
    `99% of existing code bases for client work in agencies do not.` Nope. – Jonnix Aug 29 '17 at 21:02
  • 2
    Well if you will come up with ludicrous statements... anyway... moving on. – Jonnix Aug 29 '17 at 21:04
  • Like your first comment that rephrased exactly what I asked? Yes most WordPress agencies don't use twig or smarty. Not every agency is a godsend that uses best practices. You out of all people should know that. – thatemployee Aug 29 '17 at 21:06
  • Although I didn't down-vote I can imagine that expressing your wish in the end can lead to opinion based reactions which is out off scope on this site. For this reason I will vote to close this question because it seems to attract an unwanted discussion. Perhaps, you can try to ask the question in a different form. – Stefan Aug 29 '17 at 21:53
  • 2
    @Stefan Ah yes, maybe I shouldn't have added my opinion at the end and just left it as a question only. Didn't think people on StackOverflow were so sensitive, lesson learned. Thanks for the insight. – thatemployee Aug 29 '17 at 22:53
  • Chekout the official docs: https://code.visualstudio.com/docs/languages/php https://marketplace.visualstudio.com/search?term=php&target=VSCode&category=All%20categories&sortBy=Relevance – Paul Melero Apr 05 '18 at 13:36
  • It's not possible at current moment with native functionality. Please take a look on issue: https://github.com/Microsoft/vscode/issues/1751 – ColCh Oct 26 '18 at 16:32
  • Don't think anyone's pointed this out, but if you're willing to cough up a few pennies you can get decent PHP/HTML/JS/CSS support from Devsense's PHP Tools extension for VS Code. https://marketplace.visualstudio.com/items?itemName=DEVSENSE.phptools-vscode – AndyM84 Nov 24 '18 at 05:09
  • Sadly I've had the same issue with VSCode, I switched to using Sublime Text, Atom is good and Codeanywhere is great for FTP editing. – Tomas Crofty Nov 30 '18 at 12:28
  • Atom does this perfectly – MikeeeG Mar 18 '19 at 09:20

5 Answers5

13

You can install this plugin: PHP Intelephense

Brentspine
  • 274
  • 1
  • 15
Saket89
  • 221
  • 4
  • 11
8

As far as I can tell, it does. Take a look at this php file I have created in VS Code (no extensions), with HTML inside the file with syntax highlighting for both languages. enter image description here

There is a button (It should say the word PHP) in the bottom right corner of the screen that allows you to switch the syntax highlighting and autocompletion to another language, and while this does temporarily turn off PHP syntax, it will allow you to code in HTML a lot easier.

Jodast
  • 1,279
  • 2
  • 18
  • 33
3

You can enable html support in any language by adding the following to your settings.json file:

"emmet.includeLanguages": {
    "your-language": "html",
    "rust": "html",
    "php": "html",
    "javascript": "html",
}
2

Very easy solution

enter image description here

Search emmet in settings and remove php form Emmet: Exclude Languages list
This will definitely work for everyone

Ajay Sahu
  • 186
  • 1
  • 7
  • 2
    This answer would be better if it was more descriptive. What are Emmet abbreviations and why are they excluded to begin with? I.e. what does turning them off actually do and what are we trading in for this? – Peter Kionga-Kamau Mar 29 '23 at 14:13
1

I also faced this genuine concern while learning PHP, and the only solution I came up with at the moment is clicking on PHP button on bottom right corner of VS Code screen. I am attaching the screenshot below with that PHP button highlighted so you would be able to locate it easily. Once clicked it will ask for a language you want autocomplete to work on, select HTML and it would start autocompleting html, click again to enable PHP autocomplete. I also happened to come across GitHub Pilot Extension, you need to be a student for availing this service for free or you'll have to buy membership, It is a good tool for auto complete although that is not the gist of it's use, It is something great invented for learners and also professional coders.

  • That's exactly the correct answer, it is working for me as well. This feature is built-in within VS Code and it is simply a matter of specifying the language you need syntax highlighting for. – Konkret Mar 14 '23 at 14:17
  • @Konkret I would argue that expecting users to toggle a switch for functionality that can be trivially implemented automatically in the application via configuration (potentially a missing feature), or via plugin, is not remotely practical and would probably fail every usability test that has been created by man. – Peter Kionga-Kamau Mar 29 '23 at 14:17