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 skilled programmer with expertise in Vue.js/Nux.js for front-end development and PHP Laravel for back-end development. I excel in building APIs and services, and also have experience in web server setup & maintenance. My versatile skill set allows you to develop and maintain web applications effectively, from the user interface to the server-side functionality. I love coding with never ending learning attitude, thanks for visiting danya.dk