nezbo Posted December 22, 2008 Share Posted December 22, 2008 hi all i am looking for a function that will give me the numbers + or - 5 from a given number e.g. if ($number +- 5) This runs it the number is + or - 5 Hope this makes sense... Neil Quote Link to comment https://forums.phpfreaks.com/topic/138000-solved-or-5/ Share on other sites More sharing options...
trq Posted December 22, 2008 Share Posted December 22, 2008 Then you will need to write it. Theres no such function. Quote Link to comment https://forums.phpfreaks.com/topic/138000-solved-or-5/#findComment-721252 Share on other sites More sharing options...
nezbo Posted December 22, 2008 Author Share Posted December 22, 2008 Then you will need to write it. Theres no such function. Cheers, What is the best way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/138000-solved-or-5/#findComment-721254 Share on other sites More sharing options...
genericnumber1 Posted December 22, 2008 Share Posted December 22, 2008 edit: what he said Quote Link to comment https://forums.phpfreaks.com/topic/138000-solved-or-5/#findComment-721255 Share on other sites More sharing options...
Mark Baker Posted December 22, 2008 Share Posted December 22, 2008 Hope this makes sense... Not really if ($number +- 5) This runs it the number is + or - 5 This isn't even a function, you're trying to invent a new operator (+-) But what are you actually comparing $number with? are you trying to say that $number must have a value of -5 or +5, or that it must be +/-5 from a second value? Quote Link to comment https://forums.phpfreaks.com/topic/138000-solved-or-5/#findComment-721256 Share on other sites More sharing options...
Adam Posted December 22, 2008 Share Posted December 22, 2008 Think he's trying to determine if it's positive or negative? ??? If that's the case you don't need to use some special operator, would just be: if ($num == -5) { print 'Negative!'; } elseif ($num == 5) { print 'Positive!'; } Please explain a little further what it is you're trying to achieve.. A Quote Link to comment https://forums.phpfreaks.com/topic/138000-solved-or-5/#findComment-721264 Share on other sites More sharing options...
ILMV Posted December 22, 2008 Share Posted December 22, 2008 No, what I think he wants is a function to return a list of numbers between his given number, and either + or - 5 numbers. So for example, getNumsBetween(23,5,'-') 19,20,21,22,23 // Would return getNumsBetween(23,5,'+') 23,24,25,26,27 // Would return Am I right? ILMV Quote Link to comment https://forums.phpfreaks.com/topic/138000-solved-or-5/#findComment-721268 Share on other sites More sharing options...
nezbo Posted December 22, 2008 Author Share Posted December 22, 2008 sorry i will try and explaing a bit more i want to get all the numbers 5 greater and 5 less than a given number I.e. if I have a number 9 the if statement will only allow run if it is 4,5,6,7,8,9,10,11,12,13,14 etc... Think he's trying to determine if it's positive or negative? ??? If that's the case you don't need to use some special operator, would just be: if ($num == -5) { print 'Negative!'; } elseif ($num == 5) { print 'Positive!'; } Please explain a little further what it is you're trying to achieve.. A Quote Link to comment https://forums.phpfreaks.com/topic/138000-solved-or-5/#findComment-721273 Share on other sites More sharing options...
nezbo Posted December 22, 2008 Author Share Posted December 22, 2008 you got it No, what I think he wants is a function to return a list of numbers between his given number, and either + or - 5 numbers. So for example, getNumsBetween(23,5,'-') 19,20,21,22,23 // Would return getNumsBetween(23,5,'+') 23,24,25,26,27 // Would return Am I right? ILMV Quote Link to comment https://forums.phpfreaks.com/topic/138000-solved-or-5/#findComment-721276 Share on other sites More sharing options...
Adam Posted December 22, 2008 Share Posted December 22, 2008 $num = 10; for ($i = ($num - 5); $i <= ($num + 5); $i++) { print $i . ' '; } Not tested but should work .. A Quote Link to comment https://forums.phpfreaks.com/topic/138000-solved-or-5/#findComment-721277 Share on other sites More sharing options...
trq Posted December 22, 2008 Share Posted December 22, 2008 function getnumbersaround($num, $diff=5) { return range($num-$diff, $num+$diff); } Quote Link to comment https://forums.phpfreaks.com/topic/138000-solved-or-5/#findComment-721279 Share on other sites More sharing options...
lokie538 Posted December 22, 2008 Share Posted December 22, 2008 Damn there's some talented coders here!! Quote Link to comment https://forums.phpfreaks.com/topic/138000-solved-or-5/#findComment-721281 Share on other sites More sharing options...
aschk Posted December 22, 2008 Share Posted December 22, 2008 As a side note: I have generated a function that will tell you if the $num is within a range. /** * Within function. * */ function within($num, $range = array()){ if(!$count = count($range)){ return false; } sort($range); return ($num >= $range[0] AND $num <= $range[$count-1]); } /** * EXAMPLE USAGE: constants */ if( within( 5, range( 3, 7) ) ){ echo "true"; } else { echo "false"; } /** * EXAMPLE USAGE: variables */ $x = 5; $y = $x -5; $z = $x + 5; if( within( $x, range( $y, $z) ) ){ echo "true"; } else { echo "false"; } Hopefully this will help someone. Quote Link to comment https://forums.phpfreaks.com/topic/138000-solved-or-5/#findComment-721341 Share on other sites More sharing options...
Mark Baker Posted December 22, 2008 Share Posted December 22, 2008 As a side note: I have generated a function that will tell you if the $num is within a range. As a further side note, this already exists: if (array_search(5, range( 3, 7))) { echo "true"; } else { echo "false"; } Quote Link to comment https://forums.phpfreaks.com/topic/138000-solved-or-5/#findComment-721370 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.