📖 Scenario: You are creating a simple PHP program to decide what message to show based on the weather and time of day.
🎯 Goal: Build a PHP script that uses nested if statements to check the weather and time, then prints the correct message.
📋 What You'll Learn
Create variables for $weather and $time with exact values
Add a variable $isWeekend to indicate if it is weekend
Use nested if statements to check $weather and $time
Print the correct message based on conditions
💡 Why This Matters
🌍 Real World
Nested conditions help programs make decisions based on multiple factors, like weather and time, similar to how apps show different messages or options.
💼 Career
Understanding nested conditional execution is essential for programming logic in web development, automation scripts, and many software applications.
Progress0 / 4 steps
1
DATA SETUP: Create weather and time variables
Create a variable called $weather and set it to the string "sunny". Also create a variable called $time and set it to the string "morning".
PHP
Hint
Use = to assign the string values to the variables.
2
CONFIGURATION: Add weekend indicator variable
Add a variable called $isWeekend and set it to true.
PHP
Hint
Use true without quotes for boolean true.
3
CORE LOGIC: Use nested if statements to check conditions
Write nested if statements to check if $weather is "sunny". Inside that, check if $time is "morning". If both are true, set a variable $message to "Good morning! Enjoy the sunny day!". Otherwise, set $message to "Have a nice day!". If $weather is not "sunny", set $message to "Stay indoors and relax.".
PHP
Hint
Use == to compare strings inside if conditions.
4
OUTPUT: Print the message
Write a print statement to display the value of the variable $message.
PHP
Hint
Use print($message); to show the message.
Practice
(1/5)
1.
What does nested conditional execution mean in PHP?
easy
A. Running PHP code without any conditions
B. Writing multiple if statements one after another without nesting
C. Using only one if statement without any else
D. Using an if or else statement inside another if or else block
Solution
Step 1: Understand conditional statements
Conditional statements like if and else let the program choose actions based on conditions.
Step 2: Define nested conditionals
Nested conditionals mean placing one conditional inside another to check multiple conditions step-by-step.
Final Answer:
Using an if or else statement inside another if or else block -> Option D