如何在命令行上运行带有js代码的php文件?

时间:2018-02-02 13:12:27

标签: javascript php command-line execute

我必须在我的linux docker中通过命令行执行一个php文件,我能够执行它,但问题是在同一个文件中编写的js代码没有被执行,我无法实现所需的功能。所以请指导我正确的方向。

2 个答案:

答案 0 :(得分:2)

您必须使用execpassthru并在PHP中使用Node执行js以启动JS解释器...

我刚从Mac命令行运行此测试

# Made a directory to test in
$ mkdir jsphptest && cd jsphptest

# made a test php file
$ touch test.php

# put some PHP code in the PHP file that tells it to run a javascript file
$ echo '<?php passthru("node test.js"); ?>' > test.php

# create the above references js file
$ touch test.js

# Put some JS code in the new JS file that logs some stuff into the console
$ echo 'console.log("this is javascript");' > test.js

# run the php file
$ php test.php

#outputs: this is javascript

答案 1 :(得分:-1)

javascript是基于客户端的语言,因此您无法以这种方式执行它。 Javascript正在由浏览器执行

如果你坚持执行它。看看这个链接 How do you run JavaScript script through the Terminal?