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 ?>

<?php
// Place PHP code here
?>

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

<!DOCTYPE html>
<html>
<body>

<?php
  echo "Hello World!<br>";
?>

</body>
</html>

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 a skilled programmer specializing in Vue.js/Nuxt.js for front-end development and PHP Laravel for back-end solutions. I have a strong focus on API design and development, complemented by experience in web server setup and maintenance. My versatile expertise ensures seamless creation and maintenance of web applications, covering everything from intuitive user interfaces to robust server-side functionality. Passionate about coding and driven by a lifelong learning mindset, I invite you to explore more at danyal.dk.