Tuesday, December 23, 2008

Some Thing To Remember About PHP

posted by John Blanco @ 8:17 AM


I've been messing with PHP lately, and there's definitely been things to get used to. Here are some points to keep in mind if you have a history with out high-level languages:

  • PHP is object oriented, but the API is still largely function-based. So, getting the size of an array is done with count($array) and not $array.length. Functions, functions, functions...but much of the time it's probably preferable.

  • Regular expressions aren't native. Bah. So, use preg_match("/myregexp/", $string) rather than $string =~ /myregexp/. Dissapointing. Also, why the preg? It reads pregnant to me.

  • Instead of $string.split(), use explode($string). And, since we're using so many functions, you have to remember argument order too. And, no, it's not explode($string, $separator) -- it's the opposite. If you get back your separator, you did it the wrong way. :-)

  • PHP has simpler methods for most everything as compared to, say, Ruby. So, it's simple to create directories with functions like mkdir($path). You can even call it as @mkdir($path) to suppress duplicate directory warnings. As opposed to Ruby, where you have to actually wrap that statement in a begin/rescue.

  • Here's my biggest obstacle: The string concatenation operator is dot (.), not plus (+). This gets me a lot, though I'm getting used to it now. It once caused me a few hours of debugging when I was converting a Java program to PHP. In my mind, I convince myself it's better -- less ambiguous with mathematical addition -- but, since every other language uses plus...it gets me.


Post a Comment

<< Home