PHP运行JavaScript代码 [英] php run javascript code
问题描述
是否可以运行一些javascript表达式?例如 echo eval("Math.sqrt('25')");
is it possible to run some javascript expression? for example echo eval("Math.sqrt('25')");
推荐答案
在正常情况下:
- PHP在服务器上运行
- 然后在客户端的浏览器中运行Javascript.
因此,不,PHP不可能在服务器上执行一些Javascript代码.
So, no, it's not quite possible to have PHP execute some Javascript code on the server.
但是但是,至少在PHP扩展上嵌入了Java引擎(或包装arround),因此,允许人们在服务器上从PHP执行Javascript.
But there 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.
我正在考虑的扩展名是 spidermonkey 一:在服务器上安装并启用它会允许您从PHP在服务器上执行Javascript代码.
The extension I'm thinking about is the spidermonkey one : installing and enabling it on your server will allow you to execute Javascript code, on the server, from PHP.
当然,与任何其他PHP扩展一样,您需要成为服务器的管理员才能进行安装-默认情况下,永远不会安装该服务器,因为它可以满足非常具体的需求.
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.
关于此扩展,我从未见过它在实际情况下使用过,并且尝试过它的人并不多...这是您可能想阅读的两篇文章:
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 :
- 在PHP和PECL中使用JavaScript和SpiderMonkey
- 和 SpiderMonkey:Java脚本执行者,Depuis PHP (这是法文,在我自己的博客上)
这篇关于PHP运行JavaScript代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!