user6746919
user6746919

Reputation: 67

Creating a PHP code snippet in Visual Studio Code

I'm a little confused. The file I'm editing in VScode is a php file and the code snippet below will not work if I put the snippet in the php.json, but it does, if I insert the snippet into the html.json?!

Can someone please explain why the code snippet works in one and not the other?

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

Credit goes to the original poster for the above code snippet

Upvotes: 0

Views: 4337

Answers (3)

user20489539
user20489539

Reputation: 1

  • Create a file eg: php.code-snippets in "/home/user-name/.config/Code/User/snippets/" , then paste:
{
    "PHP Tag": 
    {
        "prefix": "php",
        "body": ["<?php \n\t$1 \n?>"],
        "description": "Place the php tag."
    }
}
  • then ctrl+shift+p, choose "developer:reload window" to restart VsCode

Upvotes: 0

Btwonu
Btwonu

Reputation: 11

I would like to build upon Álvaro González's great explanation by proposing a workaround for those 99% whose outer php file context is HTML. You can add your php tag snippet to your HTML snippets.

Upvotes: 1

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146460

PHP was initially designed as a simple templating language for HTML. Where you used to have static HTML:

<p>Hello, World!</p>

… you could replace part of it with a dynamically generated value:

<p>Hello, <?=$name?>!</p>
^^^^^^^^^^          ^^^^^ HTML
            ^^^^^^        PHP

Although it's a full-fledged language nowadays, it still maintains the same basic embedded language syntax.

The PHP interpreter does not really care if the outer language is HTML or anything else because it just ignores everything that's outside PHP tags. It reads the source file and prints it as-is, but when it finds an opening PHP tag it starts parsing and executing as PHP code whatever it finds inside the tag. So it's possible to use PHP as templating engine for anything: CSS, JavaScript, XML, plain text, binary files... Whatever. PHP doesn't need to know or care.

A text editor like VisualStudio Code faces a different situation because the surrounding language is relevant to programmer thus it's relevant to editor. If it only cared about PHP code, everything outside PHP tags would neither have syntax highlighting nor code intellisense—not cool.

In practice there're two issues with that:

  • There isn't a standard reliable way to identify the outer language from within an editor.

  • Implementing code intelligence for multiple languages within the same file is a complex problem.

Since probably 99% of the times we have PHP inside HTML most editors just assume the outer language in a .php file is HTML and call it a day.


Now, why does my entry for <?php ?> in my php.json snippet file not work at all (but works when in html.json) when it's clearly a PHP tag (and clearly not an HTML tag)?

Because when you trigger the snippet you are in HTML context. Your caret is here:

<p>Hello, |</p>

… so VSCode searches in HTML snippets.

If your caret was e.g. here:

<p>Hello, <?=|?>!</p>

… it should work flawlessly because you're in PHP context thus VSCode searches in PHP snippets (try it!). But that's a pointless feature because you don't want to insert PHP tags when you're already in PHP mode—that generates invalid PHP because you cannot nest PHP tags.

Upvotes: 2

Related Questions