0
0
PHPprogramming~15 mins

Why conditional flow is needed in PHP - See It in Action

Choose your learning style9 modes available
Why conditional flow is needed
📖 Scenario: Imagine you are creating a simple program that decides what message to show based on the time of day. This is like how a smart assistant might greet you differently in the morning, afternoon, or evening.
🎯 Goal: You will build a PHP program that uses conditional flow to check the time of day and print the correct greeting message.
📋 What You'll Learn
Create a variable to hold the current hour as a number
Create a variable to hold the greeting message
Use conditional statements to set the greeting based on the hour
Print the greeting message
💡 Why This Matters
🌍 Real World
Programs often need to respond differently depending on conditions, like showing different messages or performing different tasks.
💼 Career
Understanding conditional flow is essential for any programming job because it allows software to behave smartly and adapt to user input or data.
Progress0 / 4 steps
1
Set the current hour
Create a variable called $hour and set it to the number 14 to represent the current hour in 24-hour format.
PHP
Need a hint?

Use $hour = 14; to set the hour.

2
Create a greeting variable
Create a variable called $greeting and set it to an empty string "".
PHP
Need a hint?

Use $greeting = ""; to create an empty string variable.

3
Use conditional flow to set greeting
Use if, elseif, and else statements to set $greeting to "Good morning" if $hour is less than 12, "Good afternoon" if $hour is between 12 and 17 (inclusive), and "Good evening" otherwise.
PHP
Need a hint?

Use if, elseif, and else to check the hour and assign the greeting.

4
Print the greeting message
Use echo to print the value of $greeting.
PHP
Need a hint?

Use echo $greeting; to show the greeting.