All posts
PHPOctober 1, 20267 min read

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.

F
Fepiq Team
Fepiq

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.

What is an if statement in PHP?

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
<?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.

Add an else block for the other case

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
<?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.

Comparison operators you will use all the time

A comparison operator compares two values and gives back true or false. These are the ones every beginner should know.

OperatorMeaningExampleResult
==Equal in value5 == "5"true
===Equal in value and type5 === "5"false
!=Not equal5 != 3true
>Greater than7 > 3true
<Less than7 < 3false
>=Greater than or equal4 >= 4true
<=Less than or equal4 <= 3false

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.

== versus ===: which should you use?

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
<?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.

Check many cases with elseif

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
<?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.

Combine conditions with and, or, not

Logical operators let you join several questions into one. Use && for 'and', || for 'or', and ! for 'not'.

  • && (and): true only when both sides are true.
  • || (or): true when at least one side is true.
  • ! (not): flips true to false and false to true.
php
<?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.

A shorter way: the ternary operator

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
<?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.

Use switch when comparing one value many times

If you compare the same variable against many exact values, switch can be tidier than a long chain of elseif.

php
<?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.

Common beginner mistakes

  • Using = instead of == or === inside the condition.
  • Forgetting the curly braces or a closing }.
  • Putting elseif conditions in the wrong order.
  • Writing else if conditions: else has no condition of its own.
  • Forgetting break in a switch case.

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 Beginners

Frequently asked questions

What is an if else statement in PHP?+

An 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.

What is the difference between == and === in PHP?+

== 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.

How do I check multiple conditions in PHP?+

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.

Is it elseif or else if in PHP?+

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.

When should I use switch instead of if else?+

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 Fepiq

Get new posts in your inbox

Occasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.

Keep reading

Related posts

All posts

Let's build something

Ready to ship your next product with Fepiq?

Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.