Python to PHP Equivalents

This article provides a list of common Python 2 and Python 3 commands and their PHP equivalents. While the languages have different syntax and structure, it is possible to find similar functionality in both. Keep in mind that this list is not exhaustive, and there may be other ways to achieve the same functionality in each language.

Command List

Python 2

Python 3

PHP

print "Hello, World!"
print("Hello, World!")
echo "Hello, World!";
len("Hello, World!")
len("Hello, World!")
strlen("Hello, World!");
str(42)
str(42)
(string) 42 or strval(42);
int("42")
int("42")
(int) "42" or intval("42");
float("42.5")
float("42.5")
(float) "42.5" or floatval("42.5");
range(5)
range(5)
range(0, 5);
def example_function():
def example_function():
function example_function() {}
class ExampleClass(object):
class ExampleClass:
class ExampleClass {}
import os
import os
include "os.php"; or require "os.php";
list([1, 2, 3])
list([1, 2, 3])
$array = array(1, 2, 3);
for i in range(3):
for i in range(3):
for ($i = 0; $i < 3; $i++) {}
if x == y: elif x > y:
if x == y: elif x > y:
if ($x == $y) {} elseif ($x > $y) {}
from math import sqrt
from math import sqrt
function sqrt($number) { return sqrt($number); }
list.append(4)
list.append(4)
$array[] = 4;
dict(a=1, b=2)
dict(a=1, b=2)
$dict = array("a" => 1, "b" => 2);
sorted([3, 1, 2])
sorted([3, 1, 2])
sort($array);
list.sort()
list.sort()
sort($array);
open("file.txt", "r")
open("file.txt", "r")
fopen("file.txt", "r");
file.read()
file.read()
fread($file, filesize("file.txt"));
file.close()
file.close()
fclose($file);
os.path.join('path', 'file')
os.path.join('path', 'file')
join(DIRECTORY_SEPARATOR, array('path', 'file'));
os.path.exists('file.txt')
os.path.exists('file.txt')
file_exists('file.txt');
json.dumps(obj)
json.dumps(obj)
json_encode($obj);
json.loads(json_data)
json.loads(json_data)
json_decode($json_data, true);
re.match(pattern, string)
re.match(pattern, string)
preg_match($pattern, $string);
time.time()
time.time()
time();
datetime.datetime.now()
datetime.datetime.now()
new DateTime();
try: ... except SomeError:
try: ... except SomeError:
try { ... } catch (SomeError $e) {}
raise ValueError("Error message")
raise ValueError("Error message")
throw new ValueError("Error message");
lambda x: x * 2
lambda x: x * 2
function ($x) { return $x * 2; };
map(lambda x: x * 2, list)
map(lambda x: x * 2, list)
array_map(function ($x) { return $x * 2; }, $array);
filter(lambda x: x % 2 == 0, list)
filter(lambda x: x % 2 == 0, list)
array_filter($array, function ($x) { return $x % 2 == 0; });
reduce(lambda x, y: x * y, list)
functools.reduce(lambda x, y: x * y, list)
array_reduce($array, function ($x, $y) { return $x * $y; });
zip(list1, list2)
zip(list1, list2)
array_map(null, $array1, $array2);
xrange(5)
range(5)
range(0, 5);
itertools.groupby(list)
itertools.groupby(list)
Not directly available, custom function needed.
socket.socket()
socket.socket()
socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
urllib.urlopen(url)
urllib.request.urlopen(url)
file_get_contents($url);
urlparse.urljoin(base, url)
urllib.parse.urljoin(base, url)
Not directly available, custom function needed.
subprocess.Popen(cmd)
subprocess.Popen(cmd)
proc_open($cmd, $descriptorspec, $pipes);
threading.Thread(target=func)
threading.Thread(target=func)
Not directly available, custom function needed or use a library like pthreads.

Free Online Tools

Leave a Reply