Skip to content

Convert php code to javascript code that run on browser [Experimental]

Notifications You must be signed in to change notification settings

scrawler-labs/php2js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP2JS

Convert php code to javascript code that can run on browser

Installation

package can be installed via composer

composer require piqon/php2js

Usage

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);

Demo

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!');
}

Note

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.

Changelog

v0.3.0

  • Added support for import : include 'test' now converts to import 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';
}

About

Convert php code to javascript code that run on browser [Experimental]

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages