You can see some more examples of PHP to Python at hyperpolyglot
$x = "nice";
$y = "ass";
if ($x=="nice" && $y=="ass") {
echo "quite the tooshy";
} else {
echo "Go fuck yourself";
}
x = "nice"
y = "ass"
if(x=="nice" and y=="ass"):
print("quite the tooshy")
else:
print("Go fuck yourself")
$x = array(1, 2, 3);
foreach($x as $a) {
echo $a.PHP_EOL
}
echo 'Loop is over'.PHP_EOL;
x = [1, 2, 3]
for a in x:
print(a)
print("Loop is over")
$x = array(
'uno'=>'one',
'dos'=>'two'
);
if array_key_exists('uno', $x) {
echo $x['uno'].PHP_EOL;
}
x = {
'uno':'one',
'dos':'two'
}
if x.get('uno'):
print(x['uno'])
$x = array('a', 'b', 'c');
foreach ($x as $i => $item) {
echo "$i $item\n";
}
x = ['a', 'b', 'c']
for i, item in enumerate(x):
print i, item
# 0 a
# 1 b
# 2 C
for ($i=1; $i<=5; $i++) {
echo "The number is " . $i . "<br />";
}
for i in range(5):
print("The number is %i <br />" % i)
foreach ($x as $k => $v) {
echo $k ." ". $v;
}
for k, v in x.items():
print k, v
$i=1;
while($i<=5) {
echo $i;
$i++;
}
i=1
while i <= 5:
print(i)
i+=1
function hiya() {
return "hi there";
}
def hiya():
return "hi there"
$y = (1, 2, 3)
$z = array()
foreach($y as $value) {
array_push($z, $value+1);
}
y = [1, 2, 3]
z = [x+1 for x in y]
$b ? $a : $c
a if b else c
explode(" ", "do re mi fa")
str_split("abcd")
'do re mi fa'.split()
list('abcd')
array_slice($a, 1);
array_slice($a, 2, 2);
a[1:]
a[2:4]
is_null($v);
!isset($v);
v == None
v is None
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;
}
}
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)
try {
do_something();
}
catch (ErrorException $e) {
echo 'error occured'
}
try:
do_something()
except ErrorException, e:
print('error occured')
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
array = ['lastname', 'email', 'phone']
comma_separated = ','.join(array)
$vowels = array('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U');
$onlyconsonants = str_replace($vowels, '', 'Hello World');
vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
onlyconsonants = 'Hello World'.replace(vowels, '')
strlen('hi')
len('hi')
require "functions.php";
import functions
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
}
if(i==0):
print('i equals 0')
elif(i==1):
print('i equals 1')
elif(i==2):
print('i equals 2')