Originally published on decentphp.dev — part of the Writing decent PHP course. View this lesson on the original site.

Formatting is not cosmetic.

Formatting is not cosmetic.
Good indentation shows structure, makes bugs easier to spot, and lowers review effort.

Before

This version has inconsistent spacing and cramped structure, so the control flow is harder to read quickly.

<?php

function sendReceiptIfPaid(array $order, $mailer): void {
if (($order['is_paid'] ?? false) !== true) { return; }
$email=(string)($order['customer_email']??'');
if($email===''){return;}
$mailer->send($email,'Your receipt');
}

After

The behaviour is the same, but the formatting makes the logic easy to scan top to bottom.

<?php

function sendReceiptIfPaid(array $order, $mailer): void
{
    if (($order['is_paid'] ?? false) !== true) {
        return;
    }

    $email = (string) ($order['customer_email'] ?? '');
    if ($email === '') {
        return;
    }

    $mailer->send($email, 'Your receipt');
}

Why this version is better

  • indentation makes branch boundaries obvious
  • consistent spacing reduces visual noise
  • behaviour is unchanged, but maintenance is easier

From Writing decent PHP — What decent PHP looks like