HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to PHPVariablesData TypesOperatorsStringsArraysAssociative ArraysConditions (if / else)Loops (for / foreach / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🔁 Condition & Loop
🧩 Function
🔄 Type & Conversion
PHP›Conditions (if / else)

Conditions (if / else)

Decision making with if-elseif-else, comparison, logic, and the ternary operator (?:).

Simple enough for a 5-year-old to understand

if / else

Example code
1
<?php
Start.
DefinitionStart.
2
$score = 75;
Value.
VariableValue.
3
if ($score >= 50) {
Is it 50+?
ConditionIs it 50+?
4
echo "Passed";
If yes.
Output / InputIf yes.
5
} else {
If not...
ConditionIf not...
6
echo "Failed";
...write this.
Output / Input...write this.
7
}
End.
ConditionEnd.
DefinitionVariableConditionOutput / Input
💡 Hint: Use === for strict equality; == can surprise you with type conversion.

About this topic

Conditions are especially important in PHP because web applications constantly make decisions: is the user logged in, has the form been submitted, is the data valid. A significant portion of PHP code consists of conditions.

PHP's truthiness rules are a topic that must be known: zero, empty string, the string "0", empty array, and null are considered FALSE. The fact that the string "0" is considered false is the rule that leads to the most errors — when a form field carries a value of zero, it is thought to be empty.

PHP's ability to work intertwined with HTML brings an alternative syntax for conditions: the format that closes with endif instead of curly braces is much more readable inside templates.

What you will learn

  • the if, elseif, and else structure
  • The alternative syntax and its usage in templates
  • switch and match structures
  • PHP's truthiness rules
  • The conditional operator and its short form

When to use?

Alternative syntax is preferred in template files: curly braces scattered among HTML are hard to follow, and closing with endif clearly shows which block has ended.

The match structure is one of modern PHP's most useful additions: it returns a value, performs STRICT comparison, and has no fall-through behavior. match should be preferred over switch in new code.

Common mistakes

Wrong

$qty = "0";
if ($qty) {
    echo "quantity entered";
} else {
    echo "quantity empty";
}

The string "0" is considered false in PHP; even if the user has entered a valid value, it is called "empty". Existence checking should rely on truth values rather than isset or explicit comparison.

Correct

$qty = "0";
if ($qty !== "" && $qty !== null) {
    echo "quantity entered";
} else {
    echo "quantity empty";
}

Wrong

switch ($role) {
    case "admin":
        echo "administrator";
    case "user":
        echo "user";
}

Because there is no break, switch falls through to the next case and two lines are printed together. Also, switch performs LOOSE comparison, so there may be unexpected matches. match solves both problems.

Correct

echo match ($role) {
    "admin" => "administrator",
    "user" => "user",
    default => "unknown",
};

Wrong

if ($user = "admin") {
    echo "authorized";
}

A single equals sign performs ASSIGNMENT, not comparison; the condition is always true and the $user variable gets overwritten as well. PHP does not count this as an error, the code runs incorrectly silently — this is a serious vulnerability in authorization checks.

Correct

if ($user === "admin") {
    echo "authorized";
}

Frequently asked questions

Which values are considered false in PHP?

false, number zero, float zero, empty string, the string "0", empty array, and null. Everything outside of these is considered true — even the string "false" is true. The fact that the string "0" is false is the rule that leads to the most errors.

What is the difference between match and switch?

match is an EXPRESSION, it returns a value and can be assigned. It performs strict comparison, has no fall-through behavior, requires no break, and throws an error if no case matches. match is recommended over switch in new code.

When is alternative syntax used?

When writing intertwined with HTML. Instead of curly braces, you open with a colon and close with endif; it becomes much easier to see which condition ends between HTML blocks. Curly braces are preferred in pure PHP files.

Summary

PHP's truthiness rules are special: the string "0" and an empty array are considered false, which is why existence checking is done with isset. In templates, alternative syntax is used, and match is preferred over switch in new code.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code