Search code examples
visual-studio-codecode-snippets

How to escape $ in Visual Studio Code snippet creation


It seems when using PHP variables like $_SERVER the code snippet ignores the $. For example

{
// Example:
"IP Address Test": {
    "scope": "php",
    "prefix": "iptest",
    "body": [
                "// Debugging",
                "if($_SERVER[\"REMOTE_ADDR\"]=='${1:ipaddress}'){",
                "\t//run only my ip",
                "\t$0",
                "}"
    ],
    "description": "Test only from IP address"
}

}

outputs :

// Debugging
if(_SERVER["REMOTE_ADDR"]=='xxx.xxx.xxx.xxx'){
//run only my ip

}

Solution

  • You can't use \ you have to use double $ ..

    eg.

    // Debugging
    if($$_SERVER["REMOTE_ADDR"]=='xxx.xxx.xxx.xxx'){
        //run only my ip
    
    }