php 运行 javascript 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5121832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php run javascript code
提问by kusanagi
is it possible to run some javascript expression? for example echo eval("Math.sqrt('25')");
是否可以运行一些 javascript 表达式?例如echo eval("Math.sqrt('25')");
回答by Pascal MARTIN
In normal situations :
在正常情况下:
- PHP runs on the server
- and, then, Javascript is run on the client, in the browser.
- PHP 在服务器上运行
- 然后,Javascript 在客户端的浏览器中运行。
So, no, it's not quite possible to have PHP execute some Javascript code on the server.
所以,不,不太可能让 PHP 在服务器上执行一些 Javascript 代码。
Butthere is at least on PHP extension that embed (or wrap arround)a Javascript engine, and, as a consequence, allows one to execute Javascript on the server, from PHP.
但至少在 PHP 扩展中嵌入(或环绕)了 Javascript 引擎,因此,允许从 PHP 在服务器上执行 Javascript。
The extension I'm thinking about is the spidermonkeyone : installing and enabling it on your server will allow you to execute Javascript code, on the server, from PHP.
我正在考虑的扩展是spidermonkeyone:在您的服务器上安装并启用它将允许您在服务器上从PHP 执行Javascript 代码。
Of course, like any other PHP extension, you'll need to be admin of your server, in order to install it -- and this one is never installed by default, as it answers a very specific need.
当然,与任何其他 PHP 扩展一样,您需要成为服务器的管理员才能安装它——默认情况下从不安装这个扩展,因为它满足了非常具体的需求。
About this extension, I have never seen it used in real situations, and there are not many people who tried it... here are two articles you might want to read :
关于这个扩展,我从来没有见过它在实际情况下使用过,也没有多少人尝试过……这里有两篇你可能想阅读的文章:
- Using JavaScript in PHP with PECL and SpiderMonkey
- and SpiderMonkey : Exécuter du Javascript c?té serveur, depuis PHP(this one is in french, and on my own blog)
回答by yogsma
Try this
试试这个
echo "<script language='javascript'> Math.sqrt('25') </script>"
回答by dvb
回答by jugn
put your php into a hidden div and than call it with javascript
将你的 php 放入一个隐藏的 div 中,然后用 javascript 调用它
html / php part
html/php 部分
<div id="mybox" style="visibility:hidden;"> echo sqrt(25); </div>
javascript part
javascript部分
var myfield = document.getElementById("mybox");
myfield.visibility = 'visible';
now, you can do anything with myfield... like this
现在,你可以用 myfield 做任何事情......就像这样
alert(myfield);
回答by esdot
Since PHP is a server-side scripting language that runs on the server and Javascript is a client-side scripting language that runs in a browser you would have to have the PHP generate Javascript code (the same way it generates HTML) that gets executed after the page is loaded.
由于 PHP 是一种在服务器上运行的服务器端脚本语言,而 Javascript 是一种在浏览器中运行的客户端脚本语言,因此您必须让 PHP 生成 Javascript 代码(与它生成 HTML 的方式相同),然后执行页面已加载。
回答by horatio
回答by Chenos
Run JavaScript code from PHP
从 PHP 运行 JavaScript 代码
php v8js: https://github.com/phpv8/v8js
php v8js: https://github.com/phpv8/v8js
$v8 = new V8Js;
$v8->executeString("Math.sqrt('25')"); // 5
https://github.com/chenos/execjs
https://github.com/chenos/execjs
use Chenos\ExecJs\Context;
$cxt = new Context;
$cxt->eval("Math.sqrt('25')"); // 5
回答by Alibehtm
if you have node installed on your server then you can exec this command with php
如果您的服务器上安装了节点,那么您可以使用 php 执行此命令
node a.js
and then use the console.log as output.
然后使用 console.log 作为输出。

