【问题标题】:Working with PHP shell_exec and SpiderMonkey Javascript Shell使用 PHP shell_exec 和 SpiderMonkey Javascript Shell
【发布时间】:2011-02-17 16:12:07
【问题描述】:

假设我在一个名为 plus2.js 的 JS 文件中有这段代码:

function plus2(n){
    print (n+2);
};
plus2(n);

这就是它可以通过 PHP shell_exec 执行的方式:

echo shell_exec('js -f plus2.js');

它不会返回任何东西,因为我没有将值通知给“n”。

这就是问题所在:如何通过 PHP shell_exec 将值传递给“n”?

【问题讨论】:

    标签: php javascript shell


    【解决方案1】:

    您可以使用arguments 列表:

    function plus2(n){
        print (n+2);
    };
    plus2(parseInt(arguments[0], 10));
    

    测试:

    [adrian@cheops3:~]> js test.js 1337
    1339
    

    从您的 PHP 代码中调用它:

    $result = system('js test.js 1337');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-13
      相关资源
      最近更新 更多