Jump to content

[SOLVED] + or - 5


nezbo

Recommended Posts

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?

 

 

Link to comment
https://forums.phpfreaks.com/topic/138000-solved-or-5/#findComment-721256
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/138000-solved-or-5/#findComment-721264
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/138000-solved-or-5/#findComment-721268
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/138000-solved-or-5/#findComment-721273
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/138000-solved-or-5/#findComment-721276
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/138000-solved-or-5/#findComment-721341
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.