Python code to php converter online

UNDER CONSTRUCTION – MAY NOT WORK!

Please feel free to use our FREE online Python to PHP converter. It is intended to aid in converting Python to PHP. We encourage you to use our online converter to start the process of converting Python to PHP, as it will take care of the most common usages of the language.

Remove Python Now!

With our tool, we can help a developer remove the Python requirement from your code.

Limitations

The converter is not perfect and will require a skilled developer to finish. The end product will have errors regarding nulls and not all Python statements are covered. We recommend the use of Version Control Software to ensure you have references to the original.

Need Help? We provide consulting services. Just contact us.

Advertisement

Login or Register to Remove Ads

Convert Python to PHP Online Tool

Use at your own risk. Please backup your code before using this converter. Proper Programming, LLC provides no warranty or guarantees in its use.

Is there a software converter out there that can automatically convert this python code to PHP?

#!/usr/bin/python
import math

def calcNumEntropyBits[s]:
        if len[s] = 0.0: return 0.0
        else: return -[entropy*len[s]]

def testEntropy[s]:
        print "Bits of entropy in '%s' is %.2f" % [s, calcNumEntropyBits[s]]

testEntropy['hello world']
testEntropy['bubba dubba']
testEntropy['aaaaaaaaaaa']
testEntropy['aaaaabaaaaa']
testEntropy['abcdefghijk']

dreftymac

30.3k26 gold badges115 silver badges177 bronze badges

asked Nov 7, 2010 at 5:18

Kirk OuimetKirk Ouimet

26k43 gold badges123 silver badges171 bronze badges

3

I'm not aware of any Python-to-PHP converter in the wild, but it should be a trivial task to port and the similarities are quite easy to spot:

function calcNumEntropyBits[$s] {
        if [strlen[$s] $n] {
                $prob = $n / [float]strlen[$s];
                $entropy += $prob * log[$prob]/log[2];
        }
        if [$entropy >= 0.0] return 0.0;
        else return -[$entropy*strlen[$s]];
}

function testEntropy[$s]:
        printf["Bits of entropy in '%s' is %.2f",$s,calcNumEntropyBits[$s]];

testEntropy['hello world'];
testEntropy['bubba dubba'];
testEntropy['aaaaaaaaaaa'];
testEntropy['aaaaabaaaaa'];
testEntropy['abcdefghijk'];

The last few lines in the first function could have also been written as a standard PHP ternary expression:

return [$entropy >= 0.0]? 0.0: -[$entropy*strlen[$s]];

answered Nov 7, 2010 at 5:29

9

I created a python-to-php converter called py2php. It can auto-translate the basic logic and then you will need to tweak library calls, etc. Still experimental.

Here is auto-generated PHP from the python provided by the OP.

Chủ Đề