开发者

>!= PHP operator, how to write not equal to or greater than?

How can I write not greater-than-or-equal-to in PHP?

Is it &开发者_如何学Gogt;!= ?


Isn't not greater than or equal to x the same as less than x ?


Oh, fun. In increasing order of complexity:

  1. <
  2. (a - b > 0)
  3. !(a >= b)
  4. !(a - b <= 0)
  5. !((a > b) || (a==b))
  6. !(a - b < 0) && !(a - b == 0)
  7. !((a - b < 0) || (a - b == 0)) && !(!(a < b))
  8. !(a - b < ((a * (1/a)-1) * (b * (1/b)-1))) && !(a - b == (a * (1/a)-1) * (b * (1/b)-1)))

Personally, I would reserve #8 for someone who really annoyed me. ;)


The best way to write this is

$x = 4;
$y = 6;

if($x < $y) echo "True";

// True

$x = 4;
$y = 6;

if(!($x >= $y)) echo "True";

// True


"not greater than or equal to" is equivalent to "strictly less than" which you write as <.

If you really wanted to say "not greater than or equal to" you could just write !(a >= b).


<

(less than is the same as not greater than or equal to)


Technically, you have asked two different questions - how to write A not greater than B or A equal to B and A not equal to B or A greater than B.

The statement A not greater than B or A equal to B implies:

!(A > B) || A == B

which is a tautology for:

A <= B

And A not equal to B or A greater than B implies:

A != B || A > B

which is a tautology for:

A >= B

The other answers of A < B are representative of the statement A not greater than nor A equal to B.


simply use < ?


To prove the disbelievers that less than is different than not greater or equal:

<?
$i = acos(4);
print $i."\n";
print is_nan($i)."\n";
if (4>=$i) {
    print "ge\n";
} else {
    print "nge\n";
}
if (4<$i) {
    print "lt\n";
} else {
    print "nlt\n";
}
?>

It outputs this on my system:

$ php5 nan.php 
NAN
1
ge
lt


a not greater or equal to b is equivalent to b < a


Take a look at this page: http://www.php.net/manual/en/language.operators.logical.php

It shows interesting things about operators and how to use them... I've highlighted this specific logical operators page because these, in particular, has different behaviors when you use their similars, like "||" and "or".

It's worth to take a look =)


Doing it the way you word it

!> or <>


Some simple example :

<?php 

#not lower than 5 AND not greater than 12 

if(!($nr<5)&&!($nr>12)){ }

?>


Given that you want to test that A is not equal or greater than B;

Assuming that :

A = 10;
B = 20;

For normal comparison the code would look something like this :

if(A >= B)
{
   return "A and B are equal";
}
else {
   return "A and B are not equal";
}

If the code above was executed with the given values, then we would expect that the statement A and B are not equal as 10 is not greater or equal to 20.

Now to test the reverse or the negated version of A >= B we would simply achieve it by adding the NOT symbol (!) in our expression to reverse it.

if(!(A >= B))
{
   return "A and B are not equal";
}
else {
   return "A and be are equal";
}

The expected response buy running the code above is that statement A and B are not equal will be returned.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜