Convert js-like syntax to standalone PHP code.

2.9.1 2023-08-29 09:05 UTC



Latest Stable Version Build Status StyleCI Test Coverage Code Climate

Convert js-like syntax to standalone PHP code.


In the root directory of your project, open a terminal and enter:

composer require js-phpize/js-phpize


Use compile to get PHP equivalent code to JavaScript input:

use JsPhpize\JsPhpize;

$jsPhpize = new JsPhpize();

echo $jsPhpize->compile('foo = { bar: { "baz": "hello" } }');

This code will output the following PHP code:

$foo = array( "bar" => array( "baz" => "hello" ) );

Or use render to execute it directly:

use JsPhpize\JsPhpize;

$jsPhpize = new JsPhpize();

$code = '
    // Create an object
    foo = { bar: { "baz": "hello" } };
    key = 'bar'; // instanciate a string

    return foo[key].baz;

$value = $jsPhpize->render($code);

echo $value;

This will display hello.

This library intend to is intended to allow js-like in PHP contexts (such as in template engines).

See the Phug documentation to see an implementation example as Pug expressions PHP converter.

And also check examples to see more examples (.js files are input examples js-phpize can handle and either .return show you what the engine will return for this code, or .php show you how it's compiled into PHP).