Python and PHP: A comparison of the language basics

In this table I will compare some basic programming syntax and conventions between the Python and PHP programming languages.

Programming element Python PHP
Code commenting
PHP supports Python and JavaScript conventions
# Single line comment

"""This is a multi-line comment block. This is a multi-line comment block."""

# Single line comment
// Single line comment/*This is a multi-line comment block. This is a multi-line comment block.*/
Creating a variable that contains an int: age = 25 $age = 25;
Creating a variable that contains a float (known as a “double” in PHP): probability = 0.62 $probability = 0.62;
Creating a variable that contains a string:
Python supports single, double, and triple quotes.
state = 'North Dakota'
state = "North Dakota"
state = """North Dakota"""
$state = 'North Dakota';
$state = "North Dakota";
Denoting string literals:
Python uses an ‘r’ in front of the string. PHP uses single quotes.
path = r'\path\to\some\files'
path = r"\path\to\some\files"
path = r"""\path\to\some\files"""
$path = '\path\to\some\files'
String Concatenation:
PHP uses a dot.
greeting = "Hello" + " world"

greeting = str1 + str2

$greeting = "Hello" . " world";

$greeting = $str1 . $str2;

Creating a list or array:
PHP uses array() function.
fruit_list = ['apple', 'banana', 'cherry'] $fruit_list = array("apple", "banana", "cherry");
Getting the length of a list or array: length = len(fruit_list) $length = count($fruit_list);

$length = sizeof($fruit_list);

Python dictionary vs. PHP associative array
fruit = {'a':'apple', 'b':'banana', 'c':'cherry'} $fruit = array("a" => "apple", "b" => "banana", "c" => "cherry");
Getting keys from a dictionary or associative array: fruit_keys = fruit.keys() $fruit_keys = (array_keys($fruit));
Getting values from a dictionary or associative array:
fruit_values = fruit.values() $fruit_values = (array_values($fruit));
Adding key-value pairs to a dictionary or associative array:
fruit['o'] = 'orange' $fruit['o'] = "orange";
Deleting key-value pairs from a dictionary or associative array:
del fruit['b'] unset($fruit['b']);

Python was invented in the late 1980s (1989) by Guido van Rossum. Ideas for Python were adopted from Modula-3 and Lisp.

Here is what the PHP website says about PHP:

PHP is a popular general-purpose scripting language that is especially suited to web development.

Fast, flexible and pragmatic, PHP powers everything from your blog to the most popular websites in the world.

Here is what wikipedia says about PHP:

PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language. Originally created by Rasmus Lerdorf in 1994, the PHP reference implementation is now produced by The PHP Group. PHP originally stood for Personal Home Page, but it now stands for the recursive acronym PHP: Hypertext Preprocessor.

Here is what w3schools.com says about PHP:

PHP is a powerful server-side scripting language for creating dynamic and interactive websites. PHP is the widely-used, free, and efficient alternative to competitors such as Microsoft’s ASP.

PHP is perfectly suited for Web development and can be embedded directly into the HTML code.

For more information about some of the PHP built-in functions shown above, see:
http://php.net/manual/en/ref.array.php
http://php.net/manual/en/function.count.php
http://php.net/manual/en/function.sizeof.php
http://php.net/manual/en/function.unset.php