2

可能重复:
为什么 PHP 不替换字符串中的变量?

我一直在尝试echo exec('hi.exe $file',$results,$status);从 Php 执行这一行。其中分配给 $file 的值是文件名hi.txt(即 $file = hi.txt)。

但是每次我尝试用同一行运行我的代码时,它都会显示错误$file file not found,就好像我hi.exe hi.txt在命令提示符下运行它一样。

而且,如果我尝试使用文件名而不是 php ie 中的变量运行同一行exec('hi.exe hi.txt',$results,$status),浏览器会继续执行很长时间而不给出输出。

请有人告诉我哪里出错了!

4

2 回答 2

2

您使用的是单引号,而不是双引号。更改echo exec('hi.exe $file',$results,$status);为:

echo exec("hi.exe $file",$results,$status);

或使用点,如下所示:

echo exec('hi.exe '.$file,$results,$status);

在 PHP 中,使用单引号不会$file变成hi.txt; 它只是保留为文字字符串“$file”。使用双引号或点连接实际扩展$filehi.txt

于 2012-11-16T05:12:35.793 回答
1

单引号不会扩展变量。你可能的意思是:

echo exec("hi.exe $file",$results,$status);
于 2012-11-16T05:12:45.200 回答