Javascript php code

Is it possible to run some javascript expression? egecho eval("Math.sqrt('25')");

+5
source share
7 answers

In normal situations:

  • PHP runs on the server
  • and then Javascript runs on the client in a browser.

So no, it's not entirely possible for PHP to execute some Javascript code on the server.


But at least there is a PHP extension that embeds (or wraps around) the Javascript engine and, as a result, allows you to run Javascript on the server, starting with PHP.

The extension I'm thinking of is spidermonkey : installing and enabling it on your server will allow you to execute Javascript code on a server with PHP.

, PHP, , , , .


, , , ... , , , :

+6

echo "<script language='javascript'> Math.sqrt('25') </script>"
+5

J4P5

, , , GPL.

+2

php div javascript

html/php part

<div id="mybox" style="visibility:hidden;"> echo sqrt(25); </div>

javascript

var myfield = document.getElementById("mybox");
myfield.visibility = 'visible';

- ...

alert(myfield);
+2

PHP - , , Javascript - , , JavaScript Javascript ( , HTML), .

+1

JavaScript PHP

php v8js: https://github.com/phpv8/v8js

$v8 = new V8Js;
$v8->executeString("Math.sqrt('25')"); // 5

https://github.com/chenos/execjs

use Chenos\ExecJs\Context;

$cxt = new Context;
$cxt->eval("Math.sqrt('25')"); // 5
0

All Articles