Skip to content

PHP Tutorial PHP Syntax and Output

PHP Whitespace

Lesson 16 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

Whitespace means spaces, tabs and line breaks. In PHP code, most whitespace does not matter. These two programs do exactly the same thing:

index.php
PHP
<?php
$a=1;$b=2;echo $a+$b;
Output
OUTPUT
3
index.php
PHP
<?php
$a = 1;
$b = 2;

echo $a + $b;
Output
OUTPUT
3

The second version is much easier to read. Use whitespace for people, not for PHP.

Where whitespace does matter

You cannot put a space in the middle of a name.

index.php
PHP
<?php
$my name = "Ravi"; // wrong: a space splits the name in two
$my_name = "Ravi"; // correct

Whitespace inside a string is part of the string.

index.php
PHP
<?php
echo "a   b";
Output
OUTPUT
a   b

Indentation

PHP does not need indentation, but you should always use it. The common style uses 4 spaces per level.

index.php
PHP
<?php
$age = 20;

if ($age >= 18) {
    echo "Adult";
} else {
    echo "Minor";
}
Output
OUTPUT
Adult

Whitespace outside PHP tags is output

Anything outside <?php ... ?> is sent to the browser. A space or blank line before the opening tag, or after a closing tag, becomes part of the page.

That is why a PHP-only file should start with <?php on the very first line and should not have a closing ?> tag. PHP Tags explains the reason.

lightbulb
Let your editor format the code
Editors such as VS Code and PhpStorm can format PHP on save. The style is called PSR-12, and Modern PHP Coding Style covers it.