Isaiah's code for Greetings
if / elseif / switch and ternary Examples
Hour is: 3
Hour is: 3
<?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>Isaiah's code for Greetings</h1>");
echo('<h2>if / elseif / switch and ternary Examples</h2>');
$hour = date('G'); // 'G' returns 24-hour format of an hour with leading zeros
// To see what 'G' means, please review the following URL
// https://www.php.net/manual/en/datetime.format.php
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 ($hour)
{
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;
case ($hour >= 22 && $hour <= 24):
echo('<H3>Good Night</h3>');
break;
}
echo('<br><br>');
echo('<H3>Simple IF/ELSE</h3>');
//Simple if/else statement
if ($hour < 12)
{
$amPM = "AM";
}
else
{
$amPM = "PM";
}
echo($amPM);
echo('<H3>Ternary example</H3>');
// ternary example is the same as above, only shorter
// the expression is evaluated such that if it is true then $amPM is assigned "AM" else "PM"
$amPM = ($hour < 12) ? "AM" : "PM";
echo($amPM);
include('../includes/footer.php');
?>