Skip to content
PHP Logo

What are the best practices for properly formatting and indenting PHP code to improve readability?

Properly formatting and indenting PHP code is essential for improving readability and maintainability. To achieve this, it is recommended to use consistent indentation (usually 4 spaces), follow a coding standard (such as PSR-1 or PSR-2), and separate code blocks with blank lines. This helps make the code easier to read and understand for yourself and other developers.

php
<?php

// Example of properly formatted and indented PHP code
function greet($name) {
    if ($name) {
        echo "Hello, $name!";
    } else {
        echo "Hello, stranger!";
    }
}

$name = "John";
greet($name);

?>