PHP If Else Statements Explained for Beginners (Guide)
Learn how PHP if else statements work, step by step. Simple examples, comparison operators, elseif and switch explained for absolute beginners. Start here.
Learn how PHP if else statements work, step by step. Simple examples, comparison operators, elseif and switch explained for absolute beginners. Start here.
How do you make a PHP program choose between two actions? You use an if else statement. An if else statement checks a condition (a question that is either true or false). If the answer is true, PHP runs one block of code. If it is false, PHP can run a different block instead.
In this beginner guide you will learn the exact syntax, the comparison operators, how to use elseif for more than two choices, and when to use switch. Every example is small, and every example is explained line by line. You do not need any earlier programming experience.
Programs often need to make decisions. A website might show a welcome message only when a user is logged in. A shop might give free delivery only when the cart is large enough. These decisions are called conditional logic, and in PHP they are written with if statements.
A condition is any expression that PHP can turn into true or false. A boolean is the name for a value that is only true or false. An if statement runs its code only when the condition is true.
<?php
$age = 20;
if ($age >= 18) {
echo "You are an adult.";
}Line 1 opens PHP. Line 2 creates a variable named $age (a labelled box that stores a value) and puts 20 in it. Line 4 is the if: it asks whether $age is greater than or equal to 18. Because 20 is bigger than 18, the answer is true, so PHP runs the code inside the curly braces and prints the message. If $age were 15, nothing would be printed.
Often you want something to happen when the condition is false too. That is what else is for. Think of it as the 'otherwise' part of the sentence.
<?php
$age = 15;
if ($age >= 18) {
echo "You are an adult.";
} else {
echo "You are under 18.";
}Here $age is 15, so the condition on the if line is false. PHP skips the first block and runs the else block instead, printing 'You are under 18.' Only one of the two blocks ever runs. Never both.
A comparison operator compares two values and gives back true or false. These are the ones every beginner should know.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal in value | 5 == "5" | true |
| === | Equal in value and type | 5 === "5" | false |
| != | Not equal | 5 != 3 | true |
| > | Greater than | 7 > 3 | true |
| < | Less than | 7 < 3 | false |
| >= | Greater than or equal | 4 >= 4 | true |
| <= | Less than or equal | 4 <= 3 | false |
The most common beginner mistake is using a single equals sign. One = assigns a value (it stores it in a variable). Two == or three === compares values. If you write if ($age = 18), you overwrite $age instead of checking it.
The double equals == checks only the value and lets PHP convert types behind your back. The triple equals === checks the value and the type (such as number or text). As a beginner, prefer === because it is stricter and surprises you less.
<?php
$input = "5";
if ($input == 5) {
echo "Loose match";
}
if ($input === 5) {
echo "Strict match";
}The variable $input holds the text "5", not the number 5. The first if is true because == ignores the type, so 'Loose match' prints. The second if is false because the text "5" is a different type from the number 5, so 'Strict match' does not print.
Sometimes two choices are not enough. The elseif keyword lets you add more conditions. PHP checks them from top to bottom and runs the first block whose condition is true. After that it skips the rest.
<?php
$score = 72;
if ($score >= 90) {
echo "Grade A";
} elseif ($score >= 70) {
echo "Grade B";
} elseif ($score >= 50) {
echo "Grade C";
} else {
echo "Try again";
}With $score set to 72, PHP first asks if it is 90 or more. It is not, so PHP moves on. Next it asks if it is 70 or more. That is true, so it prints 'Grade B' and stops checking. The final else catches every score below 50. Order matters here: if you checked >= 50 first, a score of 95 would wrongly get Grade C.
Logical operators let you join several questions into one. Use && for 'and', || for 'or', and ! for 'not'.
<?php
$isLoggedIn = true;
$isAdmin = false;
if ($isLoggedIn && $isAdmin) {
echo "Welcome, admin.";
} elseif ($isLoggedIn) {
echo "Welcome back.";
} else {
echo "Please log in.";
}The first condition needs both values to be true. $isAdmin is false, so it fails. PHP then tries elseif ($isLoggedIn), which is true, so the visitor sees 'Welcome back.' This pattern is very common in real websites.
For a simple two-way choice, PHP has a one-line shortcut called the ternary operator. It reads as: condition ? value if true : value if false.
<?php
$items = 0;
$message = ($items > 0) ? "Checkout" : "Your cart is empty";
echo $message;Line 3 checks if $items is above zero. Since it is 0, the condition is false, so $message gets the text after the colon. The result printed is 'Your cart is empty'. Use this only for short choices. For anything longer, a normal if else is easier to read.
If you compare the same variable against many exact values, switch can be tidier than a long chain of elseif.
<?php
$day = "Sat";
switch ($day) {
case "Sat":
case "Sun":
echo "Weekend";
break;
default:
echo "Weekday";
}PHP looks at $day and jumps to the matching case. 'Sat' has no code of its own, so PHP falls through to the next line and runs the code for 'Sun', printing 'Weekend'. The break word stops PHP from running into the following cases. The default block runs when nothing matches.
If your code behaves oddly, print the value you are testing with var_dump($variable). It shows the value and its type, which quickly reveals most mistakes.
Ready to repeat actions once your decisions are made? Next, learn how PHP loops work so you can run code many times.
Read: PHP Loops for BeginnersAn if else statement lets PHP choose between two paths. If the condition is true, the code in the if block runs. If it is false, the code in the else block runs instead.
== compares only the value and may convert types, so 5 == "5" is true. === compares the value and the type, so 5 === "5" is false. Beginners should prefer === because it is more predictable.
Use elseif to test several conditions in order, or join conditions in one line with && (and) and || (or). PHP runs the first block whose condition is true.
Both work in most cases, but elseif is the recommended single-word form. It is also required if you use the alternative colon syntax for if statements.
Use switch when you compare one variable against many exact values, such as days of the week. Use if else for ranges, like scores above 50, or for mixed conditions.
Want help building a real PHP website or learning to code with expert guidance? Talk to the Fepiq team.
Contact FepiqOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
Learn SQL CASE WHEN step by step. This beginner guide shows how to label, group and sort data with simple examples you can copy and run today.
PostgreSQL ILIKE explained for beginners: search text without worrying about case, learn % and _ wildcards, and copy simple examples you can run today.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.