{"id":13826567,"url":"https://github.com/dan-da/py2php","last_synced_at":"2025-07-12T12:32:51.104Z","repository":{"id":64025965,"uuid":"47038925","full_name":"dan-da/py2php","owner":"dan-da","description":"py2php is a utility that will auto-translate python code into PHP code.","archived":false,"fork":false,"pushed_at":"2021-10-25T16:38:52.000Z","size":107,"stargazers_count":99,"open_issues_count":8,"forks_count":32,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-11-20T05:30:54.688Z","etag":null,"topics":["php","porting","python","python-to-php","translator"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","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":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-11-28T20:45:06.000Z","updated_at":"2024-10-28T21:42:56.000Z","dependencies_parsed_at":"2023-01-14T19:30:32.118Z","dependency_job_id":null,"html_url":"https://github.com/dan-da/py2php","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dan-da%2Fpy2php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dan-da%2Fpy2php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dan-da%2Fpy2php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dan-da%2Fpy2php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dan-da","download_url":"https://codeload.github.com/dan-da/py2php/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":["php","porting","python","python-to-php","translator"],"created_at":"2024-08-04T09:01:40.428Z","updated_at":"2024-11-21T23:55:05.502Z","avatar_url":"https://github.com/dan-da.png","language":"Python","readme":"# py2php\n\npy2php is a utility that will auto-translate python code into PHP code.\n\nIt is intended as a porting aid only. You will still need to review the\ngenerated PHP and make tweaks. But it does handle a lot of the grunt work\nand common cases.\n\npy2php is a heavily modified copy of pyjs.py from the pyjamas project.\npyjs.py was written to translate python to javascript. py2php changes the\noutput semantics from JS to php.\n\n# Example\n\nHere's a simple python program to generate a sequence of fibonacci numbers.\n\n```python\ndef gen_fib(count):\n    i = 1\n    if count == 0:\n        fib = []\n    elif count == 1:\n        fib = [1]\n    elif count == 2:\n        fib = [1,1]\n    elif count \u003e 2:\n        fib = [1,1]\n        while i \u003c (count - 1):\n            fib.append(fib[i] + fib[i-1])\n            i += 1\n\n    return fib\n\nprint gen_fib(10)\n```\n\nand here is the autogenerated PHP.\n\n```php\n\u003c?php\nrequire_once('py2phplib.php');\nfunction gen_fib($count) {\n    $i = 1;\n    if (($count == 0)) {\n        $fib = array();\n    }\n    else if (($count == 1)) {\n        $fib = array(1);\n    }\n    else if (($count == 2)) {\n        $fib = array(1, 1);\n    }\n    else if (($count \u003e 2)) {\n        $fib = array(1, 1);\n        while (($i \u003c ($count - 1))) {\n            $fib[] = ($fib[$i] + $fib[($i - 1)]);\n            $i += 1;\n        }\n    }\n    return $fib;\n}\n\npyjslib_printFunc(gen_fib(10));\n```\n\nBoth produce the exact same output:\n\n```\n$ python fibonacci.py \n[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]\n```\n\n```\n$ php fibonacci.py.php \n[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]\n```\n\nThe file py2phplib.php contains some PHP functions that emulate python keywords\nsuch as print, range, etc.  py2php automatically renames these keywords to call\nthe emulated functions instead.\n\nLook in the tests directory for more examples.\n\n\n# translate_python_to_php\n\ntranslate_python_to_php is a wrapper script for py2php.  Run it in any directory\nwithout arguments and it will attempt to translate all the .py files into .php\nfiles.\n\nRun it with one or more arguments, and it will attempt to translate the files in\nthe argument list.\n\n# String Concatenation\n\nPython uses + for string concatenation and PHP uses dot (.).\n\nBecause python variables are dynamically typed, it is not possible for py2php to\nknow if a given variable represents a string or a number.\n\npy2php attempts to deal with this by:\n\n1) If either side of the expression is a string constant, then use dot operator.\n\n2) If either side of the expression is a variable that contains \"str\" or \"buf\"\nin the name, then use dot operator.\n\n3) For all other cases, use plus operator.\n\nThis means that if your string variable names do not contain \"str\" or \"buf\" then\nyou will need to manually change all related occurrences of \"+\" to \".\".\n\n# Porting Convention\n\nWhen porting python code, I have been manually creating a subdirectory\nnamed _autotranslated, into which I move all the original .py files.  I\nthen run translate_python_to_php which creates all the translated .php files. I\nthen copy (not move!) those generated files back into the parent directory for\nreview and tweaking, to finish the port.\n\nThis way, the _autotranslated directory contains only original .py files\nand the autogenerated .php files. This should make things easier in the future\nwhen the upstream python project releases a new version. Then we will simply\ncopy the new .py files into a new tmp directory, re-run the autotranslater, and\nthen we have a nice PHP \u003c--\u003e PHP diff we can use as a porting aid. Once the port\nis complete, the tmp directory files should replace the contents of\n_autotranslated directory.\n\n","funding_links":[],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdan-da%2Fpy2php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdan-da%2Fpy2php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdan-da%2Fpy2php/lists"}