使用shell_exec执行后台变量无效[关闭]

I used shell_exec command to get parallel execution of php script like this :

    shell_exec("echo \"<?php require_once 'path/to/function.php'; DBTest::function2('f2-2');?>\" | php >/dev/null 2>&1 & ");

This work correct , put in this way I can't use variable,like this:

    shell_exec("echo \"<?php require_once 'path/to/function.php'; $s =25 ; DBTest::function2('f2-2');?>\" | php >/dev/null 2>&1 & ");

it's not execute! Can help ?

Since your string is in double quotes, your $ gets expanded, before it is passed to shell_exec. See string parsing.

Escape with a backslash :

 shell_exec("echo \"<?php require_once 'path/to/function.php'; \$s =25 ; DBTest::function2('f2-2');?>\" | php >/dev/null 2>&1 & ");

In the shell, $ is also a special character. I'm not sure if you have to escape it a second time for the shell.