Glossary

Short-Circuit Evaluation

Short-circuit evaluation is an optimisation of logical expression evaluation: if the result is already known after the first operand, the second is not evaluated. Common behaviour in all programming languages.

Rules

Practical uses

// Safe check before access
if ($user && $user->isAdmin()) { ... }

// Default value — if first is falsy, return second
$name = $input['name'] ?? 'Guest';

// Conditional execution
$debug && error_log($message);

// Guard clause — stop the chain
$result = getData() || throw new NotFoundException();

Side effects

If the second operand has a side effect (DB query, log write) — it will not execute on short-circuit. Keep this in mind when designing conditions.