开发者

Working with PHP shell_exec and SpiderMonkey Javascript Shell

开发者 https://www.devze.com 2023-02-11 06:24 出处:网络
Let say I have this code in a JS file called plus2.js: function plus2(n){ print (n+2); }; plus2(n)开发者_JAVA技巧;

Let say I have this code in a JS file called plus2.js:

function plus2(n){
    print (n+2);
};
plus2(n)开发者_JAVA技巧;

That's how it can be exacuted via PHP shell_exec:

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

Which doesn't return anythig because I have not informed a value to "n".

And that's the question: how can I pass a value to "n" via PHP shell_exec?


You can use the arguments list:

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

Test:

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

To call it from your PHP code:

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

精彩评论

暂无评论...
验证码 换一张
取 消