>!= PHP operator, how to write not equal to or greater than?
58,035
Solution 1
Isn't not greater than or equal to x
the same as less than x
?
Solution 2
Oh, fun. In increasing order of complexity:
- <
- (a - b > 0)
- !(a >= b)
- !(a - b <= 0)
- !((a > b) || (a==b))
- !(a - b < 0) && !(a - b == 0)
- !((a - b < 0) || (a - b == 0)) && !(!(a < b))
- !(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. ;)
Solution 3
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
Solution 4
"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)
.
Solution 5
<
(less than is the same as not greater than or equal to)
Related videos on Youtube
Comments
-
Sam over 2 years
How can I write not greater-than-or-equal-to in PHP?
Is it
>!=
?-
Beweelam almost 14 yearsAs @John Rasch points out, @Sam is using "or", where perhaps he means "nor". @Sam, can you edit the question to read "not greater-than-or-equal-to", with hyphens, if this is what you mean? Future seekers of this answer may want to avoid confusion between or/nor.
-
psx over 10 yearsWas July 8th 2010 a Friday? Cause this is exactly what my brain does to me on a Friday afternoon too.
-
-
Mike almost 14 years@Ian P: I've added one for you ;-)
-
Jeff Fohl almost 14 yearsPerhaps this question is a good candidate for code bowling? (The more code - higher points - is the winner)
-
Piskvor left the building almost 14 years@Jeff Fohl: Good grief, no! We might get a distributed, parallelized, XML-backed, synergistic proactive network architecture (which somehow involves a wooden table, and whose sole purpose is to determine if x < y). There is a site for that, but it's not SO.
-
Mike almost 14 years@Jeff Fohl: Does that mean that if my code is not less than or equal to your code, you will win?
-
Lucas Oman almost 14 yearsI'm not going to upvote you simply because no one should be rewarded for knowing the answer to this question.
-
murgatroid99 almost 14 yearsI just have to mention that #8 will break on integer values (because of integer division) and non integer values (rounding errors messing with equality).
-
Richard Simões almost 14 yearsIt is incredible that an answer to this question would garner that much reputation.
-
Rab almost 14 years@murgatroid Good point about #8. (+1) I must remember to avoid that issue whenever I use it. Perhaps when adding redundant operations to slow down a process, in those cases where the users are known to be evil.
-
luiscubal almost 14 years@Rab isn't sleep(1) better for that?
-
ninjalj almost 14 yearsWell, not exactly. NaNs are supposed to be unordered.
-
Rab almost 14 years@luiscubal Well yeah but that would be too much like using the "<" operator ... I mean, as long as we're making things more complicated than they need to be, let's stick to it.
-
el.pescado - нет войне almost 14 yearsNew definition of mathematical induction - if something holds for 4 and 6, it is true for all integers;)
-
Lemon almost 14 years@BipedalShark, I am a bit in disbelief myself O.o
-
Michael Mrozek almost 14 yearsI used to be against weighted reputation based on question difficulty, but this is just about the greatest example in support of it I've ever seen :)
-
Helmut almost 12 yearsno, "not greater or equal" is NOT the same as "less than"... e.g. NULL or a string is "not greater or equal" to 1, however it is not less than 1 either!
-
Lemon almost 12 years@Helmut:
!(null >= 1)
,(null < 1)
,!("a" >= 1)
,("a" < 1)
, alltrue
on my machine. -
Helmut almost 12 yearswell, if
("a" < 1)
returns true, what about the opposite? In any case, from the logic side, just in my opinion, it is up to interpretation how to compare a string and an integer for which one is greater. But I like your solution!(...)
... -
Lemon almost 12 years@Helmut, I think you need to read up on logic... None of what you have said changes the fact that
!(x >= y) === (x < y)
. What you're comparing and how they are compared is irrelevant, as long asx
andy
can be compared and the comparisons are consistent. -
Helmut almost 12 yearsWell, Svish, sorry but let me try again explain what I mean: you say that not
>=
is the same as<
and say that("a" < 1)
is true. So, the opposite would be that not<=
is the same as>
, so("a" > 1)
should return true as well. In that case, "a" would be greater than 1 and smaller than 1 - do you call this logic? -
Lemon almost 12 years@Helmut, You're not making any sense. What you're comparing is irrelevant. If
!(x >= y)
istrue
, then(x < y)
is alsotrue
. If!(x >= y)
isfalse
, then(x < y)
is alsofalse
. They are equivalent. They give the same result. If they don't, you need to check the implementation of your equality and comparison operators. -
Lemon almost 12 yearsThe opposite, would be
!(x <= y)
and(x > y)
, which are also equivalent. They would give the opposite of the previously mentioned ones, unlessx
andy
are equal. Then they all returnfalse
. Just test it out yourself. -
Helmut almost 12 yearsYes, Svish... you are right. I never said that that was wrong. But still "not greater than or equal" cannot be the same as "smaller than" from a logic side, as you cannot compare a string to an integer. This would be like, speaking in images, comparing light to sound... but of course, if you are just outcome oriented as you seem to be, you say I have to equal results so it must be true... nevermind...
-
Helmut almost 12 yearsOne example, though: a function can return either a string or an integer and you need to proof wether the returned result is not greater than 1 - how will you do this with just
<
or>
? You have to go for!( )
, haven't you? -
Lemon almost 12 years@Helmut, Dude, let it go. You're not making any sense. Data types has nothing to do with the question or my answer. And even if it did, it still wouldn't matter. You can compare a number with a string without problem, you just need a comparison function that supports both.
-
Helmut almost 12 yearsSvish, obviously I just gave you an example in my last comment, where it does matter. Why don't you tell me if my assumption is right in that case rather than poorly saying "you make no sense"? In that example I gave you you cannot use only
<
or>
if you want to get the right results... so? have a nice day ;) -
Lemon almost 12 yearsYour example isn't the same as the question, so it has no relevance. Of course, if what you're after is
not greater than x
, then you'll use!(y > x)
, but that wasn't the question. The question wasnot equal to or greater than x
, which can be written as!(y >= x)
, or the equivalenty < x
. Have a nice day :) -
ninjalj over 10 years@Svish:
!(y >= x)
is not the same asy < x
. Consider NaNs, which are supposed to be unordered, and return false in every comparison (even comparing NaNs to NaNs). Incidentally, when I tested this for my answer to this question, PHP gave the wrong answer. Testing it now, it gives the right answer. -
ninjalj over 10 years@Helmut: you were right that
!(y >= x)
is not the same asy < x
, but you chose the wrong example. NaNs are a perfect example, comparisons with them should always return false. -
Lemon over 10 years@ninjalj Considering NaNs was not part of the question. If you have to deal with those, go ahead and do it another way, but for regular cases this works and is correct.
-
Lemon about 10 yearsWhat's the difference between
less than
andstrictly less than
? -
Wyatt Ward almost 9 yearsI have been writing C for weeks and fell into this trap -_-
-
Mark K Cowan almost 9 yearsWould this also give a better result for NaNs, with respect to the original question (
>!=
) -
Steve R. over 7 yearsThis proved useful as there are situations in my database where the return value was
null
. Allowed me to assign a default value:if(!($x > 0)) {$x=40;}
-
Aaron Wallentine almost 7 yearsAccording to the most common meaning, that will fail, because a value that is greater will pass the second test, since it's an or, and will therefore get through.
not greater than or equal
typically meansnot greater than AND not equal
, notnot greater than OR not equal
. But true, it is technically ambiguous as worded. I think most people would take the first meaning though. -
Nica over 6 yearsyou made my day xD