Coding Horrors: PHP’s strpos()

I decided to start a column where I will point my finger at repulsive coding and software engineering cases, which happened to be released to the unsuspecting public too hastily and / or without good thinking.  Someone will say it’s easy to criticize.  I don’t care.  Someone has to tell the plain facts and the sooner we raise our voices, and the louder, the better.  Demand for programmers has spawned numberless hordes of dim-witted script-scribblers, who claim themselves programmers, and who spill out hideous crap to the innocent audience out there.

The choice for my first rant is PHP’s function strpos().  Actually, PHP itself deserves a scorching rant, and maybe I will come to it eventually.  Right now, I will only focus on this specific function.

But what’s wrong with strpos()?  It is a simple routine, available in a basic library for virtually any language out there.  It merely checks the occurrence of a substring in a string.  And so it does.  Except for this very peculiar unique behavior, stated in the official documentation:

Returns the position as an integer. If needle is not found, strpos() will return boolean FALSE.

If you know that strings in PHP are zero-based and the frivolous nature of PHP when it comes to type-safety you will be already on your toes.  It is a realm for unspeakable bugs out there.  The warning in the official documentation, and the vast number of user comments in the official documentation page are a living proof of the disaster.  Please read these notes, really.  They would be hilarious, if the fact that something like this made it to the broad audience weren’t so sad.

I really wonder why the hell a simple function like this should possibly return values in more than one type, and for no apparent reason.  Just tell me the added value, please! 

Sadder, no one ever bothered to fix this ill behavior.  Yeah, I know compatibility will suffer.  And compatibility is important.  But when you have something that broken, hell, deprecate it!  State clearly that there is a replacement, and that this freak should no longer be used!  Instead, PHP guys got themselves busy with implementing half-assed OOP features in the language, and useless exception support.  Maybe I should just say “Go, Open Source!”.

This entry was posted on Tuesday, December 23rd, 2008 at 4:46 pm and is filed under Architecture, Just Thoughts. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

 

One Response to “Coding Horrors: PHP’s strpos()”

  1. Lirit Says:

    Badly need your help. It?s like your batteries get low, and you need to charge them on someone else?s story. Help me! Help to find sites on the: freeware. I found only this - and tithing. Debt reduction, lance was one of -also a modern rights to accumulate against the money. Debt reduction, twenty-five sides deal that more than 50 period of money debt builders are only populous to turn them just and are considering to get the service member reduction. With respect :mad:, Lirit from Madagascar.

Leave a Reply