如何在 Visual Studio 代码片段创建中转义 $

Rya*_*yan 5 code-snippets visual-studio-code

当使用像 $_SERVER 这样的 PHP 变量时,代码片段似乎会忽略 $。例如

{
// 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"
}
Run Code Online (Sandbox Code Playgroud)

}

输出:

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

}
Run Code Online (Sandbox Code Playgroud)

Rya*_*yan 8

你不能使用 \ 你必须使用 double $ ..

例如。

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

}
Run Code Online (Sandbox Code Playgroud)