SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jul 2002
    Location
    Tampa Bay
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Need very basic if-statement syntax help

    I have a variable, $h, that contains the hour (in military time) that an entry was posted. I'd like to echo a user-friendly time-of-day label depending on the value of $h. (For example, if $h is 13 -- ie, the post was entered in the 1 PM hour -- I'd like it to print "Early afternoon".) So I basically need to set up a series of if statements. This is what seems logical to me...

    PHP Code:
    if ($h 0) { $x "The midnight hour";}
    elseif (
    <= $h <= 4) { $x "Wee morning hours"; }
    elseif (
    <= $h <= 7) { $x "Early morning"; }
    elseif (
    <= $h <= 10) { $x "Mid-morning"; }
    elseif (
    $h 11) { $x "Late morning"; }
    elseif (
    $h 12) { $x "Lunchtime"; }
    elseif (
    13 <= $h <= 14) { $x "Early afternoon";}
    elseif (
    $h 15) { $x "Mid-afternoon";}

    echo 
    $x
    (Obviously there will be more, as I stopped at 3 PM, but until I get it to work, this is the basic idea.) The above code produces this error:
    Parse error: parse error, unexpected T_IS_SMALLER_OR_EQUAL in /home/hsphere/local/home/blitchre/journal.gobecky.com/index.php on line 86
    which indicates to me that I just don't have the syntax right. Can someone set me straight?

  2. #2
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd use a switch instead:
    PHP Code:
    switch($h) {
        case (
    $h == 0):
            
    $x 'The Midnight hour';
            break;
        case (
    $h <= 4):
            
    $x 'Wee morning hours';
            break;
        case (
    $h <= 7):
            
    $x 'Early morning';
            break;
        case (
    $h <=10):
            
    $x 'Mid-morning';
            break;
        case (
    $h == 11):
            
    $x 'Late morning';
            break;
        case (
    $h == 12):
            
    $x 'Lunchtime';
            break;
        case (
    $h <= 14):
            
    $x 'Early afternoon';
            break;
        case (
    $h == 15):
            
    $x 'Mid-afternoon';
            break;
        default:
            
    $x 'Some Other Time';
            break;


  3. #3
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't do (1 < $value < 10). Also you don't need to check the range because each successive check eliminates the lower values.
    PHP Code:
    if ($h 0) { $x "The midnight hour";}
    elseif (
    $h 5) { $x "Wee morning hours"; }
    elseif (
    $h 8) { $x "Early morning"; }
    elseif (
    $h 11) { $x "Mid-morning"; }
    elseif (
    $h 11) { $x "Late morning"; }
    elseif (
    $h 12) { $x "Lunchtime"; }
    elseif (
    $h 5) { $x "Early afternoon";}
    elseif (
    $h 15) { $x "Mid-afternoon";}

    // or 

    $x[0] = "The midnight hour";
    $x[1] = "Wee morning hours";
    $x[2] = $x[1];
    $x[3] = $x[1];
    $x[4] = "Early morning";

    echo 
    $x[$h]; 
    Christopher

  4. #4
    SitePoint Member
    Join Date
    Jul 2002
    Location
    Tampa Bay
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks -- both of these solutions make sense and jog my memory. I appreciate it!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •