Panasonic Youth

Adventures in PHP

Been doing a lot of PHP in anger the past two weeks. Its not the cleanest language, but you can definitely get stuff done quickly with it. I’ve been getting used to the Wordpress source, and lots of plugins and hacks built on top of it. Its pretty much at the oppositie extreme from the typical Java apps you see - a lot of simple, straightforward code, plenty of duplication, without a whole lot of thought given to abstractions or MVC. The lack of any namespacing at all means a lot of dumb function names like mycompany_find_foo and what not. The main docs at php.net are decent, but I wish they had a bit more structure enforced from something like javadoc.

The lack of any consistency for across the libraries is well known and been written about by many. Then you have some great functions like these two:

  • strpos - Returns the numeric position of the first occurrence of needle in the haystack string. Unlike the strrpos() before PHP 5, this function can take a full string as the needle parameter and the entire string will be used.
  • strrpos - Returns the numeric position of the last occurrence of needle in the haystack string. Note that the needle in this case can only be a single character in PHP 4. If a string is passed as the needle, then only the first character of that string will be used.

Of course, you have to use the strict equality operator === with those kind of functions, as you don’t know if your results of 0 means false or a position of 0 with the weak typing of php without it.

Of course, there is a lot like. PHP is incredibly pragmatic, and there is a ton of stuff in the standard library if you take the time to look. The regex support is great. Its zero deployment on mac or linux platforms - my macbook had apache and php all set and ready to go - just uncomment to lines in httpd.conf. Php.net allows comments on its documentation, something that Sun should’ve done from the start to avoid the fragmentation you see amonst the different javadoc sitest that do this.

as an aside, I still haven’t found a book that is the “Effective Java” of the PHP world. Please comment if you have suggestions.