Search code examples
phpvisual-studio-codecode-snippets

How can i have two snippets for PHP in VSCode?


This is how I imagined that I could do it but I see that it is overwritten and only the last one is working:

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

        "prefix": "echo",
        "body": [ "echo \"$1\";" ],
        "description": "php tag"
    }
}

Thanks!


Solution

  • That's not even valid JSON. You should be getting green squiggly lines that display Duplicate object key on hover:

    Duplicate object key

    You need to edit the snippets\php.json file and add several sub-objects to the top-level object identified by different key names; such key is meant to contain the snippet name, not the language, which is already set in the file name:

    {
        "PHP tags": {
            "prefix": "php",
            "body": [ "<?php $1 ?>" ],
            "description": "php tag"
        },
        "echo statement": {
            "prefix": "echo",
            "body": [ "echo \"$1\";" ],
            "description": "php tag"
        }
    }