Jump to content

small switch problem


shadowfox

Recommended Posts

edit: gah, i was looking at another post and posted in the wrong php section....

 

just making a small switch based on the hour of the day but running into a problem when the time is 00 (24hr clock). it seems to default to the dawn output. even tried to change the code so the night output would be on the top, but it still outputs as dawn when the time is at 0.

 

		$test = 0;
	switch ($test) {
		case $test >= 6 && $test <= 8:
			return "dawn(".$test.")";
			break;
		case $test >= 9 && $test <= 17:
			return "day(".$test.")";
			break;
		case $test >= 18 && $test <= 20:
			return "evening(".$test.")";
			break;
		case $test >= 21 && $test <= 23 || $test >= 0 && $test <= 5:
			return "night(".$test.")";
			break;

 

 

Link to comment
https://forums.phpfreaks.com/topic/163788-small-switch-problem/
Share on other sites

this is not how SWITCH statements work...you should probably have this as an IF statement, but if you want to stay with a SWITCH, it would look like:

 

      $test = 0;
      switch (1) {
         case $test >= 6 && $test <= 8:
            return "dawn(".$test.")";
            break;
         case $test >= 9 && $test <= 17:
            return "day(".$test.")";
            break;
         case $test >= 18 && $test <= 20:
            return "evening(".$test.")";
            break;
         case $test >= 21 && $test <= 23 || $test >= 0 && $test <= 5:
            return "night(".$test.")";
            break;

 

but you should probably just use an IF:

$test = 0;
if($test >= 21 || $test <= 5){
  return "night($test)";
}elseif($test >= 18){
  return "evening($test)";
}elseif($test >= 9){
  return "day($test)";
}else{
  return "dawn($test)";
}

Link to comment
https://forums.phpfreaks.com/topic/163788-small-switch-problem/#findComment-864227
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.