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-phpizeUse compile to get PHP equivalent code to JavaScript input:
useJsPhpize\JsPhpize;
$jsPhpize = newJsPhpize();
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:
useJsPhpize\JsPhpize;
$jsPhpize = newJsPhpize();
$code = ' // Create an object foo = { bar: { "baz": "hello" } }; key = 'bar'; // instanciate a stringreturn 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).
