Ernest's Code for Greetings
if / elseif / switch and ternary Examples
Hour is: 8
Hour is: 8
<?php
include("../includes/header.php");
// code
error_reporting(E_ALL); // set error reporting to all
date_default_timezone_set("America/Detroit"); // set time zone for here, not the server
echo("<h1>Ernest's Code for Greetings</h1>");
echo('<h2>if / elseif / switch and ternary Examples</h2>');
$hour = date('G'); // 24-hour format
echo("<p>Hour is: $hour</p>");
//========== if =========
if ($hour >= 0 && $hour < 12) {
echo('<h3>Good Morning!</h3>');
}
if ($hour >= 12 && $hour < 18) {
echo('<h3>Good Afternoon</h3>');
}
if ($hour >= 18 && $hour < 22) {
echo('<h3>Good Evening</h3>');
}
if ($hour >= 22 && $hour <= 24) {
echo('<h3>Good Night</h3>');
}
//========== if elseif =========
if ($hour >= 0 && $hour < 12) {
echo('<h3>Good Morning!</h3>');
} elseif ($hour >= 12 && $hour < 18) {
echo('<h3>Good Afternoon</h3>');
} elseif ($hour >= 18 && $hour < 22) {
echo('<h3>Good Evening</h3>');
} else {
echo('<h3>Good Night</h3>');
}
//========== switch =========
switch (true) {
case ($hour >= 0 && $hour < 12):
echo('<h3>Good Morning!</h3>');
break;
case ($hour >= 12 && $hour < 18):
echo('<h3>Good Afternoon</h3>');
break;
case ($hour >= 18 && $hour < 22):
echo('<h3>Good Evening</h3>');
break;
default:
echo('<h3>Good Night</h3>');
break;
}
echo('<br><br>');
echo('<h3>Simple IF/ELSE</h3>');
// Simple if/else
if ($hour < 12) {
$amPM = "AM";
} else {
$amPM = "PM";
}
echo($amPM);
echo('<h3>Ternary Example</h3>');
$amPM = ($hour < 12) ? "AM" : "PM";
echo($amPM);
include("../includes/footer.php");
?>