>!= 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:

  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. ;)

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)

Share:
58,035

Related videos on Youtube

Sam
Author by

Sam

studying computer science!

Updated on January 06, 2022

Comments

  • Sam
    Sam over 2 years

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

    Is it >!= ?

    • Beweelam
      Beweelam almost 14 years
      As @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
      psx over 10 years
      Was July 8th 2010 a Friday? Cause this is exactly what my brain does to me on a Friday afternoon too.
  • Mike
    Mike almost 14 years
    @Ian P: I've added one for you ;-)
  • Jeff Fohl
    Jeff Fohl almost 14 years
    Perhaps this question is a good candidate for code bowling? (The more code - higher points - is the winner)
  • Piskvor left the building
    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
    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
    Lucas Oman almost 14 years
    I'm not going to upvote you simply because no one should be rewarded for knowing the answer to this question.
  • murgatroid99
    murgatroid99 almost 14 years
    I 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
    Richard Simões almost 14 years
    It is incredible that an answer to this question would garner that much reputation.
  • Rab
    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
    luiscubal almost 14 years
    @Rab isn't sleep(1) better for that?
  • ninjalj
    ninjalj almost 14 years
    Well, not exactly. NaNs are supposed to be unordered.
  • Rab
    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 - нет войне
    el.pescado - нет войне almost 14 years
    New definition of mathematical induction - if something holds for 4 and 6, it is true for all integers;)
  • Lemon
    Lemon almost 14 years
    @BipedalShark, I am a bit in disbelief myself O.o
  • Michael Mrozek
    Michael Mrozek almost 14 years
    I 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
    Helmut almost 12 years
    no, "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
    Lemon almost 12 years
    @Helmut: !(null >= 1), (null < 1), !("a" >= 1), ("a" < 1), all true on my machine.
  • Helmut
    Helmut almost 12 years
    well, 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
    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 as x and y can be compared and the comparisons are consistent.
  • Helmut
    Helmut almost 12 years
    Well, 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
    Lemon almost 12 years
    @Helmut, You're not making any sense. What you're comparing is irrelevant. If !(x >= y) is true, then (x < y) is also true. If !(x >= y) is false, then (x < y) is also false. 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
    Lemon almost 12 years
    The opposite, would be !(x <= y) and (x > y), which are also equivalent. They would give the opposite of the previously mentioned ones, unless x and y are equal. Then they all return false. Just test it out yourself.
  • Helmut
    Helmut almost 12 years
    Yes, 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
    Helmut almost 12 years
    One 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
    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
    Helmut almost 12 years
    Svish, 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
    Lemon almost 12 years
    Your 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 was not equal to or greater than x, which can be written as !(y >= x), or the equivalent y < x. Have a nice day :)
  • ninjalj
    ninjalj over 10 years
    @Svish: !(y >= x) is not the same as y < 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
    ninjalj over 10 years
    @Helmut: you were right that !(y >= x) is not the same as y < x, but you chose the wrong example. NaNs are a perfect example, comparisons with them should always return false.
  • Lemon
    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
    Lemon about 10 years
    What's the difference between less than and strictly less than?
  • Wyatt Ward
    Wyatt Ward almost 9 years
    I have been writing C for weeks and fell into this trap -_-
  • Mark K Cowan
    Mark K Cowan almost 9 years
    Would this also give a better result for NaNs, with respect to the original question (>!=)
  • Steve R.
    Steve R. over 7 years
    This 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
    Aaron Wallentine almost 7 years
    According 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 means not greater than AND not equal, not not greater than OR not equal. But true, it is technically ambiguous as worded. I think most people would take the first meaning though.
  • Nica
    Nica over 6 years
    you made my day xD