📖 Scenario: You are creating a simple program to check the weather temperature and give advice on what to wear.
🎯 Goal: Build a PHP script that uses if statements to decide what message to show based on the temperature.
📋 What You'll Learn
Create a variable called $temperature with the exact value 15.
Create a variable called $cold_threshold with the exact value 10.
Use an if statement to check if $temperature is less than $cold_threshold.
If true, set a variable $message to 'It is cold, wear a jacket.'.
Otherwise, set $message to 'The weather is nice, no jacket needed.'.
Print the $message variable.
💡 Why This Matters
🌍 Real World
Checking conditions like weather temperature helps apps give useful advice, like what clothes to wear.
💼 Career
Understanding if statements is essential for any programming job because it helps programs make decisions.
Progress0 / 4 steps
1
Set the temperature variable
Create a variable called $temperature and set it to the number 15.
PHP
Hint
Use the $ sign to create variables in PHP.
2
Set the cold threshold variable
Create a variable called $cold_threshold and set it to the number 10.
PHP
Hint
Remember to use $ before variable names in PHP.
3
Write the if statement to check temperature
Write an if statement that checks if $temperature is less than $cold_threshold. If true, set $message to 'It is cold, wear a jacket.'. Otherwise, set $message to 'The weather is nice, no jacket needed.'.
PHP
Hint
Use if (condition) { ... } else { ... } structure in PHP.
4
Print the message
Write a print statement to display the $message variable.
PHP
Hint
Use print($message); to show the message on the screen.
Practice
(1/5)
1. What does an if statement do in PHP?
easy
A. It defines a function.
B. It repeats code multiple times.
C. It runs code only if a condition is true.
D. It stops the program immediately.
Solution
Step 1: Understand the purpose of an if statement
An if statement checks a condition and runs code only if that condition is true.
Step 2: Compare with other options
Repeating code is done by loops, functions define reusable code, and stopping the program is done by exit or die.
Final Answer:
It runs code only if a condition is true. -> Option C
Quick Check:
If statement = runs code if true [OK]
Hint: If checks condition; runs code only when true [OK]
Common Mistakes:
Confusing if with loops
Thinking if defines functions
Believing if stops the program
2. Which of the following is the correct syntax for an if statement in PHP?
easy
A. if condition { echo 'Yes'; }
B. if (condition) { echo 'Yes'; }
C. if condition: echo 'Yes'; endif;
D. if [condition] { echo 'Yes'; }
Solution
Step 1: Recall PHP if statement syntax
In PHP, the condition must be inside parentheses and the code block inside curly braces.
Step 2: Check each option
if (condition) { echo 'Yes'; } matches the correct syntax: if (condition) { ... }. if condition { echo 'Yes'; } misses parentheses, C uses alternate syntax but missing parentheses, D uses square brackets which is invalid.
Final Answer:
if (condition) { echo 'Yes'; } -> Option B
Quick Check:
Parentheses around condition = correct syntax [OK]
Hint: Always use parentheses around condition in if [OK]