Work in progress; more will be added over time.
Control Structures
PHP if; else if; else → Python if; elif; else
PHP
if ($x == 1){ // ... } else if ($x > 2){ // ... } else { // ... } // Or... if ($x == 1): // ... elseif ($x > 2): // ... else: // ... endif;Python
if x == 1: # ... elif x > 2: # ... else: # ...Ternary Operator
PHP
Syntax: $variable = (condition) ? ifConditionValue : elseValue;$y = (empty($x)) ? '$x is empty' : '$x is not empty';Python
Syntax: variable = ifConditionValue if condition else elseValuey = "x is empty" if len(x) == 0 else "x is not empty"
PHP Function → Python Definition
Without Parameters
PHP
function foo(){ return true; } foo();Python
def foo(): return True foo()With Parameters
PHP
function foo($param1=null, $param2=null, $param3=20){ // do something... } foo(null, null, 40);Python
def foo(param1=None, param2=None, param3=20): # do something... foo(param3=40)
PHP Foreach → Python For
Simple Foreach Loops
PHP
foreach ($array as $value){ echo $value."\n"; }Python
for value in array: print value del value # OptionalArray (List) Keys Only
PHP
foreach ($array as $key => $value){ echo '$array[\''.$key."']"."\n"; }Python
for key in range(len(array)): print "array["+str(key)+"]" del key # OptionalAssociative Arrays (Dictionaries)
PHP
foreach ($array as $key => $value){ echo '$array[\''.$key."'] => $value\n"; }Python
for key, value in enumerate(dictionary): print "dictionary[%s] => %s" % (key, value) del key, value # Optional
Variables
Declaration
PHP
$var = ""; // All variables must start with a dollar sign ($)Python
var = "" # Variables don't start with a dollar signMultiple Assignment
PHP
$var1 = $var2 = "";Python
(var1, var2) = ""Type Casting
PHP
$var = (str) 20; $var = (int) true; // ...Python
var = str(20) var = int(True) # ...
