Convert php code to javascript code that can run on browser
package can be installed via composer
composer require piqon/php2js
If You want to create a copiled javascript file , you can run the following code:
<?php
include __DIR__ . '/vendor/autoload.php';
/*
* Replace test.php with path of your input php file
* Replace test.js with path for your js file output
*/
\PHP2JS\PHP2JS::compileFile(__DIR__.'/test.php',__DIR__.'/test.js');
If you just want to convert a small block of code , you can run the following code:
<?php
include __DIR__ . '/vendor/autoload.php';
$code = "
$name='1';
echo 'hi';
"
\PHP2JS\PHP2JS::compile($code);
Input code :
<?php
$name = 'Pranjal';
$cars = ['BMW','Audi'];
$cars->push('Ferrari');
echo $name;
echo $cars;
function click(){
echo 'button clicked!';
}
Output code:
let name = 'Pranjal';
let cars = ['BMW', 'Audi'];
cars.push('Ferrari');
console.log( name);
console.log( cars);
function click()
{
console.log( 'button clicked!');
}
This was designed to convert php scripts to javascript code , this might not work with full blown php classes ! You can call javascript functions like console.log etc right from your php code and it will work as expected in the browser. This compiler does not support magic variables and magic functions of PHP.
- Added support for import :
include 'test'
now converts toimport test from './test.js'
You can also use import_from() php function to define path of module. Example:
import_from('test','./component/test.js');
will compile to
import test from './component/test.js';
- Added support for async function declaration via comment:
// @async
function abc(){
return 'hi';
}
will compile to
async function abc(){
return 'hi';
}