Lesson 2: Python vs PHP: Main Syntax Differences
Instead of telling you all Python syntax features, let's save some time and look only at the main differences between Python and PHP languages.
Here's the first code example in both Python and PHP.
Python
"""This is a code comment.Multi-line comment.""" a = 5 if a > 5: print('Bigger than 5') # This is inline commentelif a >= 0: print('In between 0 and 5')else: print('Negative')
PHP
/* This is a code comment. Multi-line comment.*/ $a = 5; if ($a > 5) { echo 'Bigger than 5'; // This is inline comment} elseif ($a >= 0) { echo 'In between 0 and 5';} else { echo 'Negative';}
See the key differences? Based on this example and more examples below, let's list those differences.
1. End of Statement
- Python: The end of the statement is a new line
-
PHP: Requires a semicolon
;
at the end of each statement.
# Python: no semicolonprint('Bigger than 5')
// PHP: with semicolonecho 'Bigger than 5';
2. Begin/End of Blocks
-
Python: Uses indentation (whitespace) and colons
:
to indicate the beginning and end of code blocks. -
PHP: Uses curly braces
{}
to define code blocks.
# Python: ":" instead of curly bracesif a > 5: print('Bigger than 5') print('No, really bigger')print('This is outside of IF-statement')
// PHP: curly bracesif ($a > 5) { echo 'Bigger than 5'; echo 'No, really bigger';}echo 'This is outside of IF-statement';
3. Variable Declaration and Types
Both Python and PHP are dynamically typed languages, so you don't need to specify the data type explicitly.
Also, in Python, you don't need a $
sign for variables.
# Pythona = 5if a > 5: print('Bigger than 5')
// PHP$a = 5;if ($a > 5) { echo 'Bigger than 5';}
However, the languages have different "level of strictness" about data types.
PHP Example (loosely typed):
Conversions between types are often handled automatically.
$num = '10';$result = $num + 5;
PHP automatically converts the $num
string to a number for the addition.
Python (strongly typed):
Type conversions are more strict. The language enforces type constraints, and you must explicitly convert between different types. This example throws a TypeError
Exception:
num = '10'result = num + 5
TypeError: can only concatenate str (not "int") to str
To avoid this error, the num
variable must be converted to an integer before explicitly adding it.
num = '10'result = int(num) + 5
4. String Concatenation: "+" vs "."
Python: Uses the +
operator for concatenation.
name = 'John'message = 'Hello, ' + name
PHP: Uses the .
operator for concatenation.
$name = 'John';$message = 'Hello, ' . $name;
Python also has other syntax options for concatenation and formatting strings, like f-strings, we will talk about it in the next lesson about data types.
5. Conditional Statements: Parentheses
Python: you don't need parentheses (
and )
for conditions.
if a > 5: print('Bigger than 5')
PHP: Parentheses are required.
if ($a > 5) { echo 'Bigger than 5';}
Notice: you actually can use the (condition)
syntax in Python, but it is advised only for complex conditions if it improves readability or alters the order, like (a or b) and c
.
6. Conditional Statements: Elif / Elseif
Python: uses the elif
to check multiple conditions in sequence:
if a > 5: print('Bigger than 5')elif a >= 0: print('In between 0 and 5')
PHP: uses the elseif
instead:
if ($a > 5) { echo 'Bigger than 5';} elseif ($a >= 0) { echo 'In between 0 and 5';}
7. Logical Operators
Some operators from PHP are named differently in Python.
Specifically, the logical operators &&
and ||
are called and
and or
in Python. Similarly, the logical negation operator !
is named not
in Python.
Operator (Python) | Operator (PHP) |
---|---|
and |
&& |
or |
|| |
not |
! |
if a > 5 and b < 10: print('Case 1')elif not (a < 0 or b < 0): print('Case 2')
8. Code Comments
You probably already saw this in the very first example, but I am repeating it, just in case.
Python: Single-line comments start with #
.
# This is a single-line comment a = 5 # This is a single-line comment
In Python, there is no specific syntax for creating multi-line, like /* ... */
. However, you can achieve the same effect by using triple-quoted (multi-line) strings often used as docstrings.
"""This is a multi-line comment in Python.You can use triple-quoted strings to writecomments spanning multiple lines."""
Alternatively, you can have subsequent single-line comments.
# This is a multi-line comment in Python.# You can use the # character at the beginning# of each line to create comments spanning multiple lines.
PHP: Single-line comments start with //
, and multiline comments use /* */
.
/* This is multi-line comment*/ echo 'Hi'; // This is inline comment
So, these are the most important syntax differences so you would understand the Python code when reading/writing it.
-
- 1. Tools & Your First Python Program
- 2. Python vs PHP: Main Syntax Differences
- 3. Basic Data Types: string / int / float / bool
- 4. Complex Data Types: list / tuple / set / dictionary
- 5. For/While Loops and Comprehensions
- 6. Defining Your Own Functions
- 7. Importing Libraries
- 8. CSV Files: Reading and Writing
if (a > 5) {
should beif ($a > 5) {
in PHP Conditional StatementsThank you, great notice! Fixed now.