saeed_violinist Posted November 29, 2006 Share Posted November 29, 2006 Hi,I wonder how we can compare a variable (int) to know is ist bigger or smaller than 2 other int's . I recently wrote a simple code to greet my visitors, shown as below :[code]<?phpfunction Greeting() { $time = date('G'); if($time < 4 and $time >=0 ) { return "good night"; } elseif ($time < 11 and $time >= 4 ) { return "good morning"; } elseif ($time < 16 and $time >=11 ) { return "good afternoon"; } elseif ($time >= 16 and $time < 20 ) { return "good evening"; } elseif ($time <=23 and $time >=20 ) { return "goodnight"; } else { return "error!"; }}?>[/code]o, as you see I know what to do , but the result will not satistify me ! cause when I say "($time < 11 and $time >= 4)" it will contain all nymbers between =4 up to 11, another line I say "($time < 16 and $time >=11 )" sure this will also contain <11 .I want to have this --> 0<$time< 44<$time<11....please...how can I manage this with php? Link to comment https://forums.phpfreaks.com/topic/28880-how-to-compare-variables/ Share on other sites More sharing options...
kenrbnsn Posted November 29, 2006 Share Posted November 29, 2006 Change the " and " to " && " which is the operator to use.If you want0 < $time < 44 < $time < 11What happens when $time is equal to 4?Another way of doing the functions would be:[code]<?phpfunction Greeting() { $time = date('G'); $ret = 'error!'; switch (true) { case ($time < 4 && $time >=0 ): case ($time <=23 && $time >=20 ): $ret = "good night"; break; case ($time < 11 && $time >= 4 ): $ret = "good morning"; break; case ($time < 16 && $time >=11 ): $ret = "good afternoon"; break; case ($time >= 16 && $time < 20 ): $ret = "good evening"; break; default: $ret = "error!"; } return ($ret);}?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/28880-how-to-compare-variables/#findComment-132213 Share on other sites More sharing options...
saeed_violinist Posted November 29, 2006 Author Share Posted November 29, 2006 thanks a lot ken, Im sure SWITCH will do the jub, but for now :[code]<?phpfunction Greeting() { $time = date('G'); if(3<$time<4) { return "hehe"; }}?>[/code]will produce an error message : Parse error: parse error, unexpected '<' in E:\xampp\eclipse\workspace\saba\data\inc.php on line 80really how can I have something like " 4<$i<8 " ??thanks.. Link to comment https://forums.phpfreaks.com/topic/28880-how-to-compare-variables/#findComment-132240 Share on other sites More sharing options...
kenrbnsn Posted November 29, 2006 Share Posted November 29, 2006 That syntax is incorrect. You want something like:[code]<?phpif ($time > 3 && $time < 5) // this will only be true when $time is 4if ($time > 4 && $time < 8) // this will match 5, 6, or 7?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/28880-how-to-compare-variables/#findComment-132248 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.