Need basic PHP if-statement help

cleo

macrumors 65816
Original poster
Jan 21, 2002
1,186
0
Tampa Bay Area, FL, USA
I feel like such a dork... two years ago I could whip up the most complicated PHP functions with no problem, but I got away from programming for awhile and everything just slipped out of my head... it's like I'm a newbie again.

So I come to you for 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:
if ($h = 0) { $x = "The midnight hour";}
elseif (1 <= $h <= 4) { $x = "Wee morning hours"; }
elseif (5 <= $h <= 7) { $x = "Early morning"; }
elseif (8 <= $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?
 

Mitthrawnuruodo

Moderator emeritus
Mar 10, 2004
13,805
174
Bergen, Norway
First I see: Try if ($h == 0)... if not you actually set $h to 0 and that argument ($h = 0) will always be true... ;)

And, I think it should be something like: elseif (($h >=2) & ($h <= 4))... or very similar, don't have my php book within arms reach at the moment... :eek:

Second, a switch($h) could be better than lots of elseif's:

switch($h){
case 1: { $x = "The midnight hour"; break; }
case 2: ;
case 3: ;
case 4: { $x = "Wee morning hours"; break; }
case 5: ;
...
case 7: { $x = "Early morning"; break; }
...
case 24: { ...; break; }
default: { $x = "The clock cannot be that..."; }
}
 

plinden

macrumors 68040
Apr 8, 2004
3,968
3
Why not set up an array, then return $array[$h]? It'll avoid all those nasty if-elses or switches and also be faster.

Sorry I can't give an exact example - I haven't used PHP for about four years (thank god) so I'm not absolutely sure that would work.
 

cleo

macrumors 65816
Original poster
Jan 21, 2002
1,186
0
Tampa Bay Area, FL, USA
plinden said:
Why not set up an array, then return $array[$h]? It'll avoid all those nasty if-elses or switches and also be faster.

Sorry I can't give an exact example - I haven't used PHP for about four years (thank god) so I'm not absolutely sure that would work.
Ah, I totally didn't think of that! Just tried it, and for the record, it does work, perfectly in fact. It looks like...

$x[0] = "The midnight hour";
$x[1] = "Wee morning hours";

... and so on, through $x[23]. Then I just echo $x[$h]. Perfecto! (And you're right, much cleaner, too.) Thanks!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.