Austin's code for Greetings

if / elseif / switch and ternary Examples

Hour is: 14

Good Afternoon

Good Afternoon

Good Afternoon



Simple IF/ELSE

PM

Ternary example

PM

Code follows


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Greeting</title>
    <link rel="stylesheet" href="css/styles.css">
</head>
<body>

<div class="page">

    <p class="nav"><a href="../index.php">Home</a></p>

    <?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>Austin'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');
    
?>

    <br>

    <h2>Code follows</h2>

    <div class="code"><?php highlight_file(__FILE__); ?></div>
    <p class="nav nav-foot"><a href="../index.php">Home</a></p>

</div>

</body>
</html>