{"id":13653348,"url":"https://github.com/dan-da/php2js","last_synced_at":"2025-07-12T12:32:52.186Z","repository":{"id":72068157,"uuid":"47001377","full_name":"dan-da/php2js","owner":"dan-da","description":"php2js is a tool that can automatically translate PHP code into Javascript code.","archived":false,"fork":false,"pushed_at":"2016-05-20T03:15:07.000Z","size":402,"stargazers_count":23,"open_issues_count":0,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-11-10T04:36:24.864Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dan-da.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2015-11-27T22:19:23.000Z","updated_at":"2023-10-04T15:21:33.000Z","dependencies_parsed_at":"2024-01-14T14:30:35.971Z","dependency_job_id":"560e8c66-86ba-4af9-b009-6900d316b2b9","html_url":"https://github.com/dan-da/php2js","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dan-da%2Fphp2js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dan-da%2Fphp2js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dan-da%2Fphp2js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dan-da%2Fphp2js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dan-da","download_url":"https://codeload.github.com/dan-da/php2js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225820222,"owners_count":17529139,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T02:01:09.036Z","updated_at":"2024-11-21T23:55:21.413Z","avatar_url":"https://github.com/dan-da.png","language":"PHP","readme":"# php2js\n\nphp2js is a tool that can automatically translate PHP code into Javascript code.\n\nI originally wrote this back in 2008 and am just now (2015) getting around to\nreleasing it on github.\n\nI wrote this as part of a larger project, phpwt, which is a PHP port of pyjamas,\nwhich is itself a port of GWT to python.\n\nThe code has not been maintained for years and the language translation will\nnever be 100% due to differing language features and libraries, but I am\nreleasing it anyway in case someone finds it useful.\n\n## An example of code translation:\n\n### Original PHP Code\n```php\nfunction test1() {\n\n    $arr = array('colors' =\u003e array(\"red\" =\u003e 'Red', 'green' =\u003e \"Green\"),\n                 'candies' =\u003e array('twix','snickers', 'jolly ranchers'),\n                 'holiday' =\u003e 'christmas'\n                 );\n\n    $arr['planet'] = 'Earth';\n\n    $okay = $arr['colors']['red'] == 'Red' \u0026\u0026\n            $arr['candies'][1] == 'snickers' \u0026\u0026\n            $arr['holiday'] == 'christmas';\n\n    echo \"Test that assoc array init and deref works\u003cbr\u003e\\n\";\n    echo 'Result: ' . (($okay === true) ? 'pass' : 'fail') . \"\\n\\n\";\n\n}\n\ntest1();\n```\n\n### Translated Javascript code\n\n```js\ntest1 = function() {\nvar arr = ___array([ \"__kv\", \"colors\",___array([ \"__kv\", \"red\",\"Red\"],[ \"__kv\", \"green\",\"Green\"])],[ \"__kv\", \"candies\",___array(\"twix\",\"snickers\",\"jolly ranchers\")],[ \"__kv\", \"holiday\",\"christmas\"]);\narr[\"planet\"] = \"Earth\";\nvar okay = arr[\"colors\"][\"red\"]==\"Red\"\u0026\u0026arr[\"candies\"][1]==\"snickers\"\u0026\u0026arr[\"holiday\"]==\"christmas\";\n___echo(\"Test that assoc array init and deref works\u003cbr\u003e\\n\");\n___echo(\"Result: \"+((okay===true)?\"pass\":\"fail\")+\"\\n\\n\");\n\n}\n\ntest1();\n```\n\n## Supports standard PHP functions!\n\nA cool thing is that most of the built-in PHP functions work as expected after\ntranslation to javascript.\n\nFor example, sprintf(), extract(), implode(), date(), array_keys(), array_merge()\nand on and on.  These work because the translator automatically includes the\nphp.js library, from the fine folks at phpjs.org.\n\n# Why use it?\n\nThis tool can be useful any time you have code already written in PHP that\nyou would like to translate into JS.\n\nTypically this would be when moving code from server-side to the browser.\n\nOne possible use case could be to have a set of validation routines that are\nwritten in PHP for the server, but a build system automatically translates them\nto javascript so that the same validation logic can be made client side without\nrequiring server interaction.\n\n\n# Requirements\n\n\n* PHP 5.5+\n* [PHC PHP Compiler](http://www.phpcompiler.org/)\n* [SpiderMonkey Javascript engine](https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey) or nodejs.\n\nnote: spidermonkey (or nodejs) is only needed for running the test cases from the\ncommand line.\n\n# Installing\n\n1) Clone the php2js repo.\n\n2) Install PHC if not already present.\n\n3) Install Spidermonkey if not already present.  On Ubuntu, just\n\n```\nsudo apt-get install libmozjs-24-bin\n```\n \n4) Generate the test cases.\n\n```\ncd phpwt_tests\n./gen_all_tests\n```\n\n5) Run the test cases\n\n```\n./test_runner\n```\n\n\nNote: as of this writing, 88 test cases pass, and 11 are failing.\n\n\n# Usage\n\n```\nphp2js   -- a tool to translate PHP code into JS code.\n\nUsage:\n    php2js [options] \u003cphp-file\u003e [include-paths]\n    \nOptions:\n    --help               Display usage info.\n    --exclude-phpjs      Do not include phpjs javascript library in output.\n```\n\nTranslated javascript will be printed to STDOUT and may be redirected to a file.\n\nBy default, the generated javascript will contain the phpjs function library and\nyour translated code will be at the end.\n\nIn a future version, hopefully we can remove any unused library functions to\nkeep the generated output smaller.\n\n\n\n","funding_links":[],"categories":["PHP"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdan-da%2Fphp2js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdan-da%2Fphp2js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdan-da%2Fphp2js/lists"}