1

I was asking to myself what was the best way to compare between :

$count < 3

and

$count <= 2

or if it was exactly the same, no matter what.
I'm talking in PHP but I guess it's the same for a lot of language.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
Seekme
  • 174
  • 8
  • I dont think it is going to make any difference! – Rahul Tripathi Apr 22 '15 at 08:05
  • If $count is an integer, the result will be the same, but in term of performances, I don't know. – Morb Apr 22 '15 at 08:06
  • Those two should be equivalent, I think. `$count < $length` and `$count <= $length - 1` should not (as you have an extra subtraction). The difference is tiny and probably not worth obsessing over. – Amadan Apr 22 '15 at 08:06
  • In C++ there would be essentially no difference between the two, q.v. [this SO article](http://stackoverflow.com/questions/12135518/is-faster-than). PHP is interpreted, so you would have to know how the interpreted code would handle it, but it's likely analogous to most assemblers. – Tim Biegeleisen Apr 22 '15 at 08:11
  • 6
    I would use the one that makes most sense when reading the code. If I want an if-loop for values lower than 50, I want to see 50 in my code for easy reference, not 51. – RST Apr 22 '15 at 08:15
  • I prefer `x <= n` than `x < n+1` because php should do nothing to compare condition, in case `x < n+1` php should calculate result each time... – cn007b Apr 22 '15 at 10:24
  • I was just using _n+1_ in order not to use a specific number, but my exemple is what I really mean. @RST comment makes sense, thanks ! – Seekme Apr 22 '15 at 12:06

0 Answers0