Skip to content

Exercise 4 of 10 · Conditions

Open or Closed

What you will make

A message that changes depending on the hour stored in a variable — 'open' or 'closed' — decided by the program itself.

The one new idea: if/else with && requiring every part of a condition to be true

A program that always says the same thing isn't very useful. if/else is how it looks at a value and decides which of two things to do, which is the first step toward code that reacts to real data.

Go straight to the code ↓

Choosing between two outcomes

if lets a program run one block of code only when a condition is true, and else gives it something to run instead when that condition is false. Together, if/else is a strict either/or: exactly one of the two blocks runs, never both, never neither.

Conditions are often built from comparisons like >= and <, combined with logical operators. && (and) requires every part of a condition to be true before the whole thing counts as true. || (or) only needs one part to be true — which makes it a poor fit for a rule like "between two numbers," where both halves genuinely need to hold at the same time.

A worked example

Here's a different range check, so you can see the pattern with fresh numbers:

PHP
<?php
$age = 15;

if ($age >= 13 && $age < 20) {
    echo "Teenager.\n";
} else {
    echo "Not a teenager.\n";
}

Running it prints:

Output
Teenager.

Both halves of the condition have to hold for someone to count as a teenager: at least 13, and under 20. Swap that && for || and almost every age would satisfy at least one half, making the condition true far too often.

Your turn

The starter program is supposed to report a shop as open only during a specific window of hours, and closed the rest of the time. Right now it reports the shop as open even at an hour well outside that window.

Look at the logical operator joining the two comparisons inside the if. Decide whether the rule — open only when both the lower and upper bound hold — needs "both must be true" or "either one is enough," then fix the operator and press Run.

If something goes wrong

If the shop reports as open no matter what hour you try, the condition is probably using || where it needs && — with ||, almost any hour satisfies at least one of the two comparisons, so the whole thing reads as true far too often. If PHP behaves strangely and $hour seems to change value unexpectedly, check for a stray single = inside the condition; a single = assigns a value instead of comparing one, which is a classic PHP trap.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Reaching for || when the rule really needs both conditions
|| only needs one side to be true, so a range check like 'between 9 and 18' almost always needs && instead — with ||, nearly every hour satisfies at least one half and the condition ends up true almost always.
Using = instead of == or === inside a condition
A single = assigns a value rather than comparing one; PHP allows it, so if ($hour = 9) silently sets $hour to 9 and the condition just reflects whatever 9 evaluates to (true), instead of checking anything.
Forgetting the else branch runs whenever the if is false
Every if with an else is a strict choice between two paths — if the condition isn't true, the else block runs no matter how false it ended up being.

Want a blank editor instead? Open the PHP playground.