Ryan
Ryan

Reputation: 642

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

}

Upvotes: 5

Views: 981

Answers (2)

jlewis90
jlewis90

Reputation: 51

I can't add a comment, but Mark has the correct answer.

$$ places the cursor at the beginning of the $ once VSCode has rendered the string. \ correctly places the cursor where you want it instead of one at every $. So, instead of...

<--cursor-here-->$_SERVER

// before $ gives us

$_SERVER<--cursor-here-->

Upvotes: 0

Ryan
Ryan

Reputation: 642

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

eg.

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

}

Upvotes: 8

Related Questions