Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

php to python code converter

8,725 views
Skip to first unread message

bvidinli

unread,
May 8, 2009, 6:38:25 AM5/8/09
to pytho...@python.org

Pascal Chambon

unread,
May 8, 2009, 11:19:13 AM5/8/09
to pytho...@python.org
Hello

That's funny, I was precisely thinking about a php to python converter,
some weeks ago.
Such a tool, allowing for example to convert some CMS like Drupal to
python, would be a killer app, when we consider the amount of php code
available.

But of course, there are lots of issues that'd have to be fixed :
- translating the php syntax to python syntax
- forcing scope limitations where php doesn't have any
- handling differences in semantics (for example, the booleanness of "0"
or)
- handling the automatic variable creation and coertion that php features
- handling the php types like arrays (which are neither python lists nor
python dicts)
- providing a whole mirror of the php stdlib (string and file functions,
access to environment vars...)

Some things, like PECL modules, would imo be almost impossible to handle
(there are already so much trouble to make Cpython extensions available
to other python implementations...), but I guess that 95% of existing
php websites could be wholly translated "just" with a language
translator and an incomplete stddlib replacement.

That's hell a lot of work anyway, so it'd be worth weighing it ^^

Actually, your module "phppython" might already be rather useful,
because I've crossed here and there people desperately asking for help
when translating some php function of their own to python.

But if the project were to become bigger, I guess some choices would
have to be rechecked. For example it seems you parse the php code your
own way, instead of using existing php parsers ; I think the most
flexible way would be walking some kind of php abstract syntax tree, and
translating it to python AST on the way. Also, writting the comments in
english woudl be mandatory :p

I'd like to have the opinion of people around : do you think that
complete language translators like php<->python or ruby<->python are
possible ? Impossible ? Not worth the effort ? Or must be reached by
another way (eg. Parrot and stuffs) ?

Regards,
Pascal

PS : Am I the only one having most of answers rejected by the antispam
system of python-list ? That's humiliating :p

bvidinli a �crit :

> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
>

D'Arcy J.M. Cain

unread,
May 8, 2009, 11:37:20 AM5/8/09
to Pascal Chambon, pytho...@python.org
On Fri, 08 May 2009 17:19:13 +0200
Pascal Chambon <chambon...@gmail.com> wrote:
> That's funny, I was precisely thinking about a php to python converter,
> some weeks ago.
> Such a tool, allowing for example to convert some CMS like Drupal to
> python, would be a killer app, when we consider the amount of php code
> available.

I'm not a big fan of PHP but I don't understand the desireability of
such a tool. If you have a good PHP app just run it. The point of
Python in my mind is that it is a cleaner syntax and promotes better
code. Anything that converts PHP to Python is going to leave you with
some butt-ugly Python. It also risks adding new bugs.

If you want a Python version of Drupal then perhaps that is the project
that you want to start. Start fresh using the existing project as your
requirements specification. Don't automatically import all their
coding choices that may be partially based on the language used.

--
D'Arcy J.M. Cain <da...@druid.net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.

Dotan Cohen

unread,
May 8, 2009, 12:33:10 PM5/8/09
to D'Arcy J.M. Cain, pytho...@python.org, Pascal Chambon
> I'm not a big fan of PHP but I don't understand the desireability of
> such a tool.  If you have a good PHP app just run it.  The point of
> Python in my mind is that it is a cleaner syntax and promotes better
> code.  Anything that converts PHP to Python is going to leave you with
> some butt-ugly Python.  It also risks adding new bugs.
>
> If you want a Python version of Drupal then perhaps that is the project
> that you want to start.  Start fresh using the existing project as your
> requirements specification.  Don't automatically import all their
> coding choices that may be partially based on the language used.
>

It would be a great learning tool. I am pretty decent with PHP but
just getting started in Python. This might help such users flatten out
the learning curve.

--
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il

D'Arcy J.M. Cain

unread,
May 8, 2009, 1:28:45 PM5/8/09
to Dotan Cohen, pytho...@python.org, Pascal Chambon
On Fri, 8 May 2009 19:33:10 +0300
Dotan Cohen <dotan...@gmail.com> wrote:
> It would be a great learning tool. I am pretty decent with PHP but
> just getting started in Python. This might help such users flatten out
> the learning curve.

If there were such a tool available I would sugegst it come with huge
warnings suggesting that the resulting code not be used for learning
purposes. You would still be much better off with the tutorials.

Diez B. Roggisch

unread,
May 8, 2009, 1:43:50 PM5/8/09
to
Dotan Cohen wrote:

I'm almost 100% sure it won't. The code is machine-generated, thus not easy
to the human eye, not idiomatic so you don't learn how to write good python
through it. You learn how to jump through hoops to code the same way in
python that you do in PHP.

Diez

Dotan Cohen

unread,
May 8, 2009, 2:03:35 PM5/8/09
to Diez B. Roggisch, pytho...@python.org
> I'm almost 100% sure it won't. The code is machine-generated, thus not easy
> to the human eye, not idiomatic so you don't learn how to write good python
> through it. You learn how to jump through hoops to code the same way in
> python that you do in PHP.
>

I meant for single functions, not for translating entire apps.

J. Cliff Dyer

unread,
May 8, 2009, 4:03:13 PM5/8/09
to Pascal Chambon, pytho...@python.org
On Fri, 2009-05-08 at 17:19 +0200, Pascal Chambon wrote:
> PS : Am I the only one having most of answers rejected by the
> antispam
> system of python-list ? That's humiliating :p
>

I've had several messages not make it through.

:(


MRAB

unread,
May 8, 2009, 5:06:48 PM5/8/09
to pytho...@python.org
Same here ("Message has a suspicious header").

Diez B. Roggisch

unread,
May 9, 2009, 10:14:17 AM5/9/09
to
Dotan Cohen schrieb:

>> I'm almost 100% sure it won't. The code is machine-generated, thus not easy
>> to the human eye, not idiomatic so you don't learn how to write good python
>> through it. You learn how to jump through hoops to code the same way in
>> python that you do in PHP.
>>
>
> I meant for single functions, not for translating entire apps.

No difference. Either the functions are trivial, or they become
non-idiomatic.

Diez

Aahz

unread,
May 10, 2009, 8:27:05 AM5/10/09
to
In article <mailman.5314.1241816...@python.org>,

I've been trying to investigate several instances of posts to
comp.lang.python.announce getting approved and making it out to the
mailing list but not the newsgroup. In each case, reposting as plain
text fixed the problem. I suggest that anyone having similar problems on
c.l.py do the same thing.
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/

"It is easier to optimize correct code than to correct optimized code."
--Bill Harlan

sk...@pobox.com

unread,
May 10, 2009, 9:11:30 AM5/10/09
to Aahz, pytho...@python.org

>>>> PS : Am I the only one having most of answers rejected by the
>>>> antispam system of python-list ? That's humiliating :p
>>>
>>> I've had several messages not make it through.
>>>
>>> :(
>>
>> Same here ("Message has a suspicious header").

Aahz> I've been trying to investigate several instances of posts to
Aahz> comp.lang.python.announce getting approved and making it out to
Aahz> the mailing list but not the newsgroup. In each case, reposting
Aahz> as plain text fixed the problem. I suggest that anyone having
Aahz> similar problems on c.l.py do the same thing.

The problem lies with moi. The ham/spam database had grown rather huge. I
tossed out a bunch of older ham and spam messages from the database and
retrained. That caused a large fraction of new posts to be held as
"unsure". Unfortunately, I didn't notice that for a couple days. I added a
couple hundred of the recently held hams and spams to the database and
retrained last night. This morning nothing new was held. Hopefully that
will solve the problem.

--
Skip Montanaro - sk...@pobox.com - http://www.smontanaro.net/
America's vaunted "free press" notwithstanding, story ideas that expose
the unseemly side of actual or potential advertisers tend to fall by the
wayside. Not quite sure why. -- Jim Thornton

k...@fiber-space.de

unread,
May 10, 2009, 2:05:49 PM5/10/09
to
On 8 Mai, 17:19, Pascal Chambon <chambon.pas...@gmail.com> wrote:
> Hello
>
> That's funny, I was precisely thinking about a php to python converter,
> some weeks ago.
> Such a tool, allowing for example to convert some CMS like Drupal to
> python, would be a killer app, when we consider the amount of php code
> available.
>
> But of course, there are lots of issues that'd have to be fixed :
> - translating the php syntax to python syntax
> - forcing scope limitations where php doesn't have any
> - handling differences in semantics (for example, the booleanness of "0"
> or)
> - handling the automatic variable creation and coertion that php features
> - handling the php types like arrays (which are neither python lists nor
> python dicts)
> - providing a whole mirror of the php stdlib (string and file functions,
> access to environment vars...)

Some thoughts.

1) Syntax. Not a big deal.

2) Semantics. My favourite approach was to create a Python framework
that represents PHP in Python and enables round-trips. So one could
translate forth and back. Python code that is compliant to the
conventions of the framework can also be translated to PHP and for
each PHP program P following equation holds:

py2php(php2py(P)) = P

This makes readable code mandatory.

3) PHP stdlib via C bindings ( ctypes? )

4) Corner cases of bindings: cut them off. Not everything has to be
translated. But produce stubs that raise NotImplementedError
exceptions.

Same arguments apply to Javascript. Not sure about Ruby but I do think
a parser is feasible despite context sensitivities. Ruby is not my
concern though.

Personally I'd be interested in Wordpress which I like and use.

Kay

bvidinli

unread,
May 13, 2009, 12:12:56 PM5/13/09
to pytho...@python.org
i designed php to python converter a few monthos ago, to translate
some of my ready made code snippets to python.
i was experienced with php and was learning python.

as stated on project home, it is only intended for translating small
code snippets or small functions to python.

whole applications, cannot be converted using that...

only an idea, a very beta/alpha package,

since there are huge amount of php applications, files, codes all
around web, this would be a good tool if developed and used
properly...

par...@gmail.com

unread,
Aug 27, 2015, 10:33:28 AM8/27/15
to
<?php
require_once('nusoap.php');

//parameters
$param = array(
'user_id'=>'5a194cff-­‐c913-­‐3c79-­‐5415-­‐551a4fbc4ec5',
'password' =>'hTJLnXH6ZZyqLw',
'fname'=>'ali',
'lname' =>'ghaderi',
'email' =>'in...@parsishop.ir',
'quantity' =>1,
'product' =>'ENAHE',
'update_type' =>1,
'country_code' =>1022,
'debug' =>1);

//create object that referer a web services
$client =new nusoap_client('https://store.esetme.com/remote/webshop.php');

//call a function at server and send parameters too
$response =$client->call('CreateNewLicense',$param);
//process result
if($client->fault){
echo "FAULT:<P>Code:".$client->faultcode."</p>";
echo "string:".$client->faultstring;
}else{
if($response['error']==")}
//Request is Successful
//Handle Response Data
echo $response=['username'];
echo<br/>;
echo $response=['password'];
echo<br/>;
echo $response=['expiration_data'];

else{
//an error occurred
//handle error
echo$response=['error'];



}



?>

Chris Angelico

unread,
Aug 27, 2015, 10:57:28 AM8/27/15
to pytho...@python.org
On Fri, Aug 28, 2015 at 12:33 AM, <par...@gmail.com> wrote:
> [ block of PHP code with no explanation ]

I surmise from your subject line that you want a Python program that
does the same thing? Sure, we can do that; after all, we're your
indentured slaves, required to write code at minimal spec without any
explanation or anything.

#!/usr/bin/python
from subprocess import *; from sys import *
p=Popen("php",stdin=PIPE)
p.stdin.write(''.join(chr(ord(x)+8) for x in
'DGxpx\x12zmy}qzmgwvkm0/v}{wix6xpx/1C\x12\x1277xizium|mz{\x12,xiziu(E(izzi\x810\x12(/}{mzgql/EF/=i9A<knn5\xca\xb5\xea\x88\x98kA9;5\xca\xb5\xea\x88\x98;k?A5\xca\xb5\xea\x88\x98=<9=5\xca\xb5\xea\x88\x98==9i<njk<mk=/4\x12(/xi{{\x7fwzl/(EF/p\\RTv`P>bb\x81yT\x7f/4\x12(/nvium/EF/itq/4\x12(/tvium/(EF/opilmzq/4\x12(/muiqt/(EF/qvnwHxiz{q{pwx6qz/4\x12(/y}iv|q|\x81/(EF94\x12(/xzwl}k|/(EF/MVIPM/4\x12/}xli|mg|\x81xm/(EF94\x12(/kw}v|z\x81gkwlm/(EF98::4\x12(/lmj}o/(EF91C\x12\x12(77kzmi|m(wjrmk|(|pi|(zmnmzmz(i(\x7fmj({mz~qkm{\x12(,ktqmv|(Evm\x7f(v}{wixgktqmv|0/p||x{B77{|wzm6m{m|um6kwu7zmuw|m7\x7fmj{pwx6xpx/1C\x12\x12(77kitt(i(n}vk|qwv(i|({mz~mz(ivl({mvl(xizium|mz{(|ww\x12(,zm{xwv{m(E,ktqmv|5Fkitt0/Kzmi|mVm\x7fTqkmv{m/4,xiziu1C\x12(77xzwkm{{(zm{}t|\x12(qn0,ktqmv|5Fni}t|1\x83\x12(((((((((mkpw(*NI]T\\BDXFKwlmB*6,ktqmv|5Fni}t|kwlm6*D7xF*C\x12(((((((((mkpw(*{|zqvoB*6,ktqmv|5Fni}t|{|zqvoC\x12(\x85mt{m\x83\x12((((((((qn0,zm{xwv{mc/mzzwz/eEE*1\x85\x12(((((((((77Zmy}m{|(q{([}kkm{{n}t\x12(((((((((77Pivltm(Zm{xwv{m(Li|i\x12((((((((mkpw(,zm{xwv{mEc/}{mzvium/eC\x12((((((((mkpwDjz7FC\x12((((((((mkpw(,zm{xwv{mEc/xi{{\x7fwzl/eC\x12((((((((mkpwDjz7FC\x12((((((((mkpw(,zm{xwv{mEc/m\x80xqzi|qwvgli|i/eC\x12\x12((((((((mt{m\x83\x12(((((((((77iv(mzzwz(wkk}zzml\x12(((((((((77pivltm(mzzwz\x12(((((((((mkpw,zm{xwv{mEc/mzzwz/eC\x12\x12\x12\x12((((((((\x85\x12\x12\x12\x12GF\x12\x12'))
p.stdin.close()
exit(p.wait())

I believe this will do what you require.

ChrisA

Joel Goldstick

unread,
Aug 27, 2015, 11:07:17 AM8/27/15
to pytho...@python.org
> --
> https://mail.python.org/mailman/listinfo/python-list


Chris, I think you are missing a left paren
--
Joel Goldstick
http://joelgoldstick.com

Chris Angelico

unread,
Aug 27, 2015, 11:21:26 AM8/27/15
to pytho...@python.org
On Fri, Aug 28, 2015 at 1:06 AM, Joel Goldstick
<joel.go...@gmail.com> wrote:
> Chris, I think you are missing a left paren

Borrow one of the ones from inside the quoted string, there's plenty
of spares. They won't notice one missing. I'm fairly sure the rest of
the code is syntactically valid, though.

But I did get a sign backward. I'll leave it to the OP to figure that part out.

ChrisA

Joel Goldstick

unread,
Aug 27, 2015, 11:49:22 AM8/27/15
to pytho...@python.org
A quick google search led me here:

http://www.diveintopython.net/soap_web_services/index.html

That may help you out.

word...@gmail.com

unread,
Oct 5, 2017, 8:35:56 PM10/5/17
to
On Friday, May 8, 2009 at 3:38:25 AM UTC-7, bvidinli wrote:
> if anybody needs:
> http://code.google.com/p/phppython/

The link is down.
I'd like to give a plug to mynewly released product `pyx.php` Python module:

https://wordpy.com/pyx/php/

https://github.com/wordpy/pyx


Run Converted PHP Codes in Python with the Speed of Compiled-C

pyx.php is a Cython compiled module that you can use to convert or translate 99% of most common PHP source codes to pure Python. In another words, it is a PHP-to-Python syntax emulation library in Cython.

The converted Python codes only require a Python 3.x interpreter, the modules in the pyx repository, and some standard Python libraries. PHP interpreter is not needed at all.

If we have already translated most of the WordPress core and other scripts from PHP to Python using pyx.php, you can convert almost any PHP code into Python.

With the speed of compiled Cython, running Python code translated from PHP using pyx.php might be even faster than running the original PHP code in the same computer.
Installation

$ git clone https://github.com/wordpy/pyx/

Currently, pyx.php is only available for Python 3.x running 64-bit Linux. Python 2.x, Mac, or other platforms can be compiled when there are many requests.
Quick Start

$ python # or ipython

>>> import pyx.php as Php; array = Php.array
>>> arr1 = array( (0,'1-0'),('a','1-a'),('b','1-b'),)
>>> arr2 = array( (0,'2-0'),( 1,'2-1'),('b','2-b'),('c','2-c'),)
>>> arr1 + arr2 # same as: Php.array_plus(arr1, arr2), see below
>>> Php.array_merge(arr1, arr2)

....

balbdrbh...@gmail.com

unread,
Mar 1, 2020, 12:54:12 AM3/1/20
to
On Friday, May 8, 2009 at 3:38:25 AM UTC-7, bvidinli wrote:
this link doesn't work

Souvik Dutta

unread,
Mar 1, 2020, 9:44:29 AM3/1/20
to
Use this instead (I know the name is misleading)
https://pypi.org/project/convert2php/
> --
> https://mail.python.org/mailman/listinfo/python-list
>

Terry Reedy

unread,
Mar 1, 2020, 1:05:27 PM3/1/20
to
I believe free code.google.com was shut down a few years ago. I have a
file there and was emailed to make sure I had a copy before it was deleted.


--
Terry Jan Reedy

bobim...@gmail.com

unread,
Mar 22, 2020, 4:42:41 PM3/22/20
to
On Friday, May 8, 2009 at 1:38:25 PM UTC+3, bvidinli wrote:
> if anybody needs:
> http://code.google.com/p/phppython/

hi

bobimapexa

unread,
Mar 23, 2020, 3:15:29 PM3/23/20
to

anjulak...@gmail.com

unread,
May 13, 2020, 11:31:50 AM5/13/20
to
On Friday, 8 May 2009 16:08:25 UTC+5:30, bvidinli wrote:
> if anybody needs:
> http://code.google.com/p/phppython/

$username = "username";
$password = "password";
$hostname = "localhost";

$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
$selected = mysql_select_db("dropdownvalues", $dbhandle) or die("Could not select examples");
$choice = mysql_real_escape_string($_GET['choice']);

$query = "SELECT * FROM dd_vals WHERE category='$choice'";

$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) {
echo "<option>" . $row{'dd_val'} . "</option>";
}

Palash Bauri

unread,
May 14, 2020, 12:09:45 AM5/14/20
to
That gives a 404 🤷‍♂️, you sure , you pasted the right link ?

~Palash Bauri
> --
> https://mail.python.org/mailman/listinfo/python-list
>

MRAB

unread,
May 14, 2020, 8:03:39 AM5/14/20
to
On 2020-05-13 17:53, Palash Bauri wrote:
> That gives a 404 🤷‍♂️, you sure , you pasted the right link ?
>
> ~Palash Bauri
>
> On Wed, 13 May 2020, 9:07 pm , <anjulak...@gmail.com> wrote:
>
Look at the date of the original post. It says "8 May 2009". That's over
11 years ago!

Since then, Google Code has ceased to exist.

Jon Ribbens

unread,
May 14, 2020, 8:58:51 AM5/14/20
to
On 2020-05-14, MRAB <pyt...@mrabarnett.plus.com> wrote:
> Look at the date of the original post. It says "8 May 2009". That's over
> 11 years ago!
>
> Since then, Google Code has ceased to exist.

Disgraceful, all URLs should continue to work for at least as long as
this one has: http://info.cern.ch/hypertext/WWW/TheProject.html ;-)

DL Neil

unread,
May 15, 2020, 3:14:39 PM5/15/20
to
Is there then a lesson to be learned here?

Should you place trust in a company that (repeatedly) behaves in such a
fashion, yet feels entitled to lecture others about 'what is good for
the web'?
--
Regards =dn

Michael P

unread,
Nov 22, 2022, 7:50:22 PM11/22/22
to
On Friday, May 8, 2009 at 6:38:25 AM UTC-4, bvidinli wrote:
Here is another tool, if you need.
https://properprogramming.com/tools/converters/free-online-php-to-python-converter/

Michael P

unread,
May 30, 2023, 7:53:02 PM5/30/23
to
We now have many tools
Python To PHP Online (Our Guide: Step By Step / Python to PHP Equivalents)

Version 1 – Server Side
https://properprogramming.com/tools/converters/free-online-php-to-python-3-converter/
Version 2 – JavaScript & Private
https://properprogramming.com/tools/converters/python-3-to-php-converter-version-2-online/
PHP to Python (Python to PHP Equivalents)

Version 1 – Server Side
https://properprogramming.com/tools/converters/free-online-php-to-python-3-converter/
Version 2 – JavaScript & Private
https://properprogramming.com/tools/converters/online-php-to-python-3-converter-version-2/
0 new messages