php is rubbish

By k

I mean, not completely rubbish – it’s not as if you can’t develop pretty good applications using it. But…My main gripe?

strpos()

int strpos(string $haystack, mixed $needle [,int $offset])

Ok, what’s wrong with it? Well, in my experience, it would seem very natural to write the following:

  $x = "blah";
  $y = "x";
  if (strpos($x, $y)>=0) {
    print "x contains y";
  }

and expect it to output nothing.

But … it outputs “x contains y”. Why? Because strpos($x, $y) returns false (1), and false>=0 (2).

(1) is certainly wrong in my mind (why not return -1, or even null?)

(2) is rather ugly, but I can deal with it.

Leave a Reply