c_shelswell Posted June 6, 2007 Share Posted June 6, 2007 Frying my brain a bit trying to figure this out. I'm sure it's very easy though. I'm just trying to make a simple more than or less than statement but i'm sure there's a better way than the way i'm doing it: $y = 3; if ($y== 1||$y==2|| $y == 3 || $y == 4 || $y==5) { do something } so i'm just trying to make the "if" go 2 either side of 3. Is there a better way than writing it in such a long way? Thanks Link to comment https://forums.phpfreaks.com/topic/54434-simple-more-than-less-than-if-statement-i-think/ Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 $y=4; if ($y < 3 || $y > 3) { echo 'Y is not 3'; } Something like that, or what exactly are you trying to do, because really the statement you wrote does not go with how you described it working. Link to comment https://forums.phpfreaks.com/topic/54434-simple-more-than-less-than-if-statement-i-think/#findComment-269190 Share on other sites More sharing options...
c_shelswell Posted June 6, 2007 Author Share Posted June 6, 2007 sorry should have said "2 either side of 3 and inclusive of 3" Cheers Link to comment https://forums.phpfreaks.com/topic/54434-simple-more-than-less-than-if-statement-i-think/#findComment-269195 Share on other sites More sharing options...
taith Posted June 6, 2007 Share Posted June 6, 2007 <? $y = 4; if($y<>3){ echo 'i work'; } ?> Link to comment https://forums.phpfreaks.com/topic/54434-simple-more-than-less-than-if-statement-i-think/#findComment-269197 Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 sorry should have said "2 either side of 3 and inclusive of 3" Cheers <?php $y=3; if (($y < 3 || $y>=3) && ($y>0 && $y<6)) { echo 'here we are'; } ?> Not sure if that is any better than the original statement but should work. Link to comment https://forums.phpfreaks.com/topic/54434-simple-more-than-less-than-if-statement-i-think/#findComment-269203 Share on other sites More sharing options...
c_shelswell Posted June 6, 2007 Author Share Posted June 6, 2007 Yeah guess it's much the same - thanks very much for your help Link to comment https://forums.phpfreaks.com/topic/54434-simple-more-than-less-than-if-statement-i-think/#findComment-269205 Share on other sites More sharing options...
taith Posted June 6, 2007 Share Posted June 6, 2007 you can simplify that frost... <?php $y=3; if($y<>3 && ($y>0 && $y<6)){ echo 'here we are'; } ?> Link to comment https://forums.phpfreaks.com/topic/54434-simple-more-than-less-than-if-statement-i-think/#findComment-269207 Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 you can simplify that frost... <?php $y=3; if($y<>3 && ($y>0 && $y<6)){ echo 'here we are'; } ?> Doesn't the <> mean not equal to three ??? Or am I mistaken, if so he said he would like to include three in his reply... Link to comment https://forums.phpfreaks.com/topic/54434-simple-more-than-less-than-if-statement-i-think/#findComment-269210 Share on other sites More sharing options...
taith Posted June 6, 2007 Share Posted June 6, 2007 oh... sorry... i thought 3 was to be excluded... mybad... Link to comment https://forums.phpfreaks.com/topic/54434-simple-more-than-less-than-if-statement-i-think/#findComment-269212 Share on other sites More sharing options...
c_shelswell Posted June 6, 2007 Author Share Posted June 6, 2007 You can see why i was confused Link to comment https://forums.phpfreaks.com/topic/54434-simple-more-than-less-than-if-statement-i-think/#findComment-269216 Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 Just because I am bored, if you wanted this to be very versatile you could do something like this: <?php function within($num, $within=3, $within_what=2) { $upper = $within_what + $within; $lower = $within_what - $within; $x; for ($i=$within;$i<$upper;$i++) { if ($i == $num) { return true; }elseif ($x == $num) { return tre; } } return false; } $y = 4; if (within($y)) { echo 'yep'; } $y=7; if (!within($y, 3, 2)) { echo 'nope'; } $y=3; if (within($y)) { echo 'yep'; } ?> Just something fun to do if you are going to be using this quite a bit. Link to comment https://forums.phpfreaks.com/topic/54434-simple-more-than-less-than-if-statement-i-think/#findComment-269221 Share on other sites More sharing options...
c_shelswell Posted June 6, 2007 Author Share Posted June 6, 2007 great cheers i'll give that a go!! Link to comment https://forums.phpfreaks.com/topic/54434-simple-more-than-less-than-if-statement-i-think/#findComment-269222 Share on other sites More sharing options...
AV1611 Posted June 6, 2007 Share Posted June 6, 2007 sorry should have said "2 either side of 3 and inclusive of 3" Cheers $y=3; $ll=$y-2; $ul=$ul-2; if($y < $ll || $y > $ul) { echo 'T';} else { echo 'F';} Link to comment https://forums.phpfreaks.com/topic/54434-simple-more-than-less-than-if-statement-i-think/#findComment-269225 Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 <?php function within($num, $within=3, $within_what=2) { $upper = $within_what + $within; $lower = $within_what - $within; if($num < $lower || $num > $upper) { return true; }else { return false; } } $y = 4; if (within($y)) { echo 'yep'; } $y=7; if (!within($y, 3, 2)) { echo 'nope'; } $y=3; if (within($y)) { echo 'yep'; } ?> Thanks to av1611 for providing that, made the function less processing intense. =) Updated the function without the for loop. Link to comment https://forums.phpfreaks.com/topic/54434-simple-more-than-less-than-if-statement-i-think/#findComment-269233 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.