Is their a simple way to write this code?

if (($daytime >= 9) and ($daytime <= 21)) {do something}

This works fine but I was wandering if their was a way to make it simpler like: If ($daytime == 9 to 21) do something ? I tried asking Google and couldn’t fined anything last night but a found this forum!

Hi

create array for 9 to 21 and check that $daytime is in that array.

$array = array(9,10,11,12,13,14,15,16,17,18,19,20,21);

if(in_array($daytime, $array)){
//do something
}

Or, even easier…

if($daytime >= 9 && $daytime <= 21)

Thanks PHP_Person that is definitely a little bit simpler haha! I guess this is the right way to right this code.

Thanks sarthakpatel for demonstrating the array for some reason I always think their tricky! Their really not I just don’t use them enough.

Sponsor our Newsletter | Privacy Policy | Terms of Service