Skip to content

Instantly share code, notes, and snippets.

@JohnStuartRutledge
Last active December 18, 2015 00:18
Show Gist options
  • Save JohnStuartRutledge/5695373 to your computer and use it in GitHub Desktop.
Save JohnStuartRutledge/5695373 to your computer and use it in GitHub Desktop.
Side by side examples of how you would write the same code in Python and PHP.

PHP to Python

You can see some more examples of PHP to Python at hyperpolyglot


PHP

$x = "nice";
$y = "ass";

if ($x=="nice" && $y=="ass") {
    echo "quite the tooshy";
} else {
    echo "Go fuck yourself";
}

Python

x = "nice"
y = "ass"

if(x=="nice" and y=="ass"):
    print("quite the tooshy")
else:
    print("Go fuck yourself")

PHP

$x = array(1, 2, 3);
foreach($x as $a) {
    echo $a.PHP_EOL
}
echo 'Loop is over'.PHP_EOL;

Python

x = [1, 2, 3]
for a in x:
    print(a)
print("Loop is over")

PHP

$x = array(
    'uno'=>'one', 
    'dos'=>'two'
);
if array_key_exists('uno', $x) {
    echo $x['uno'].PHP_EOL;
}

Python

x = {
    'uno':'one', 
    'dos':'two'
}
if x.get('uno'):
    print(x['uno'])

PHP

$x = array('a', 'b', 'c');
foreach ($x as $i => $item) {
    echo "$i $item\n"; 
}

Python

x = ['a', 'b', 'c']
for i, item in enumerate(x):
    print i, item 

# 0 a
# 1 b
# 2 C

PHP

for ($i=1; $i<=5; $i++) {
    echo "The number is " . $i . "<br />";
}

Python

for i in range(5):
    print("The number is %i <br />" % i)

PHP

foreach ($x as $k => $v) {
    echo $k ." ". $v;
}

Python

for k, v in x.items():
    print k, v

PHP

$i=1;
while($i<=5) {
    echo $i;
    $i++;
}

Python

i=1
while i <= 5:
    print(i)
    i+=1

PHP

function hiya() {
    return "hi there";
}

Python

def hiya():
    return "hi there"

PHP

$y = (1, 2, 3)
$z = array()
foreach($y as $value) {
    array_push($z, $value+1);
}

Python

y = [1, 2, 3]
z = [x+1 for x in y]

PHP

$b ? $a : $c

Python

a if b else c

PHP

explode(" ", "do re mi fa")
str_split("abcd")

Python

'do re mi fa'.split()
list('abcd')

PHP

array_slice($a, 1);
array_slice($a, 2, 2);

Python

a[1:]
a[2:4]

PHP

is_null($v);
!isset($v);

Python

v == None
v is None

PHP

class Customer {
    private $first_name, $last_name;
 
    public function setData($first_name, $last_name) {
        $this->first_name = $first_name;
        $this->last_name = $last_name;
    }
 
    public function printData() {
        echo $this->first_name . " : " . $this->last_name;
    }
}

Python

class Customer(object):
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name  = last_name

    def printData(self):
        print '{0} : {1}'.format(self.first_name, self.last_name)

PHP

try {
    do_something();
}
catch (ErrorException $e) {
    echo 'error occured'
}

Python

try:
    do_something()
except ErrorException, e:
    print('error occured')

PHP

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

Python

array = ['lastname', 'email', 'phone']
comma_separated = ','.join(array)

PHP

$vowels = array('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U');
$onlyconsonants = str_replace($vowels, '', 'Hello World');

Python

vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
onlyconsonants = 'Hello World'.replace(vowels, '')

PHP

strlen('hi')

Python

len('hi')

PHP

require "functions.php"; 

Python

import functions

PHP

switch ($i) {
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
}

Python

if(i==0):
    print('i equals 0')
elif(i==1):
    print('i equals 1')
elif(i==2):
    print('i equals 2')

PHP

Python

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment