PHP vs Python #1: Syntax

Let’s take a look at the syntax of both languages.

PHP HelloWorld PHP example

A PHP script can be placed anywhere in the HTML document. PHP scripts start with <?php and ends with ?>

// Place PHP code here

The default file extension for PHP files is “helloworld.php“.

<!DOCTYPE html>

  echo "Hello World!<br>";


Python HelloWorld PHP example

Python syntax can be executed by writing directly in the Command Line, let’s look at an example.

>>> print("Hello, World!")
Hello, World!

Python script can be execute by storing a file with extension .py

Code Indentation

PHP in order to make code readability, code indentation is necessary, whereas in Python the indentation is very important and is used to indicate a block of code.

In PHP spacing and indentation should be consistent throughout the code. Many developers choose to use 4-space or 2-space indentation. In PHP, each nested statement (e.g., a statement following a “{” brace) should be indented exactly once more than the previous line’s indentation.

Example of PHP code indentation

Example of Python code indentation

The number of spaces is up to a programmer, but it has to be at least one. And code indent spaces must be the same in the same block of code, otherwise, Python will break.

Author: Danyal
I'm working as programmer since 2002. Born, grew up, studied and worked in Pakistan now Im located in Denmark since 2006.