Why PHP Will Be My Next Language

I've never liked PHP.

I first looked at PHP over decade ago and did not like what I saw. No, my response was stronger than that. I vehemently loathed what I saw. The syntax was ugly, nearly all samples of PHP code seemed unmaintainable, and the liberal intermixing of PHP/HTML/JavaScript/SQL syntax (Tim Bray referred to it in On PHP as "Spaghetti SQL wrapped in spaghetti PHP wrapped in spaghetti HTML, replicated in slightly-varying form in dozens of places") offended my JSP sensitivities. Everything about PHP smelled like a language for hackers, not for real software developers. Even the story of PHP's nerdy recursive acronym (PHP Hypertext Preprocessor) seemed to irritate me. It was loathing at first sight.

Although I form very strong opinions regarding software development as I learn lessons from experience, I also endeavor to keep an open mind and be willing to even change my mind sometimes, especially if the thing I have formed an opinion about changes. I blogged about an example of this in How I Learned to Stop Worrying and Love Java's Ternary Operator. Several observations have led me to think that I need to look closer at PHP again and that perhaps my opinion of PHP may improve. In this post, I look at these interrelated observations that have led me to decide to give PHP another look.

Prevalence

PHP has become a highly prevalent programming language on the web. PHP Usage Stats indicate a general upward trend in PHP usage. On the current TIOBE Programming Community Index (April 2011), PHP is the fifth-most popular programming language. I see .php in URLs all over the place. Both anecdotal and more objective evidence indicates that PHP has become a widely popular language. Knowing the basics of PHP and having some PHP experience cannot help but make a developer's skills more generally applicable and add a meaningful tool to the developer's tool set.

Rounding Out the Top Five

As I mentioned above, PHP is the fifth most popular language on the Tiobe Programming Community Index. It happens to be the only one of the top five that I have never used in any significant way. I've used the first four (listed in order they appear on the index: Java, C, C++, and C#) significantly more than PHP. I've worked quite a bit with Java, C++, and C, and have worked enough with C# to feel comfortable with it, but I have not put similar effort into using or learning PHP because of my initial adverse response to it.

Web 2.0 is PHP's Home Turf

The web has become home turf for PHP. Both Facebook and Amazon Elastic Compute Cloud, for example, provide PHP libraries and APIs as primary exposure mechanisms. The Facebook open source page states regarding PHP, "PHP is an incredibly popular scripting language which makes up the majority of our code-base. Its simple syntax lets us move fast and iterate on products." The Facebook SDKs and Tools page features the open source (generally Apache 2 license) PHP SDK. The other listed Facebook SDKs are either for mobile devices operating systems or for the JavaScript SDK or Python SDK. The C# SDK is not listed presumably because it's in alpha release. Facebook stopped supporting its "official" Java SDK in February 2008 reportedly because of the availability of third-party Facebook Java APIs such as facebook-java-api. Even the movie The Social Network featured PHP code in some scenes.

I've been reading the book Host Your Web Site In The Cloud: Amazon Web Services Made Easy: Amazon EC2 Made Easy. All the examples in this book are in PHP. The Amazon Web Services PHP Developer Center contains more articles and references on using PHP with Amazon Web Services (including the AWS SDK for PHP). There is a series of articles on developerWorks regarding Cloud Computing with PHP.

Drupal's adoption for Whitehouse.gov's 2009 overhaul was big news for highly popular Drupal. Although use of Drupal does not require thorough knowledge of PHP, knowledge of PHP can be very helpful when using Drupal because PHP is an integral part of Drupal's technology stack. An example is the ability to include PHP snippets that can be inserted into a Drupal application (JavaScript snippets and SQL snippets are the other allowed code snippets).

Not Just for Hackers Anymore

I think that many of us who dabbled a bit with PHP years ago have been left with a stubborn impression that PHP is for hackers. In Holub on Patterns, Allen Holub wrote, "A determined individual can write garbage code in any language." I have found this to be true in any programming language that I have used in any significant way, but it seemed pervasive in PHP code even without needing to spend much time in that environment. I have reason to believe that things may have been improved or may be improving in PHP.

PHP 5 introduced true object-oriented principles. "Design patterns" and "PHP" have even started to be used in the same sentence. I was encouraged to see that the book PHP in Action talks about subjects such as objects, design, unit testing, and refactoring in relation to PHP.

I had already decided to try PHP out again, but was even more motivated to do so after reading Why PHP was a Ghetto. Post author Kenny Katzgrau writes about "why the non-PHP development world generally has such disdain for PHP and the community surrounding it." His list of reasons all apply, but I also think he nails the main reason when he writes, "PHP code bases tend to be hacky and unmaintainable." None of this was a surprise to me because it certainly has been the opinion of just about every non-PHP developer I have known, but I was also encouraged to read this:

PHP was a ghetto. But the development of frameworks like Zend and CodeIgniter have greatly pushed the language development into the right direction. ... The fact is, a PHP applications can be as well-written as an application in any other language, and probably have the additional advantage of speed. The widespread use of MVC-style development in the PHP world is a relatively recent phenomena though, and admittedly, we can probably thank Rails for it.

Because Katzgrau seems to understand the source of disdain for PHP in the past, I'm hoping that his positive assessment of where it is today and the direction it is moving is correct. It sure sounds promising!

One of the reasons I like to learn new languages every once in a while is to adopt practices from those languages in my coding and to better "think outside of the box." I have to admit that I have not previously been interested in learning any PHP because I didn't want to learn any of that or be tainted by any of those practices. I'm less fearful now of the negative influence of immersing myself in this language now.

Practicality

In the previously mentioned blog post Why PHP Was a Ghetto, post author Kenny Katzgrau wrote, "I’m no PHP fanboy — in fact, I’m very language-agnostic. I write PHP more often because, you guessed it, people pay me to." In the end, my renewed interest in PHP comes down to my believing that it offers the most obvious practical advantages at this point in time. Had I not gotten into Groovy, I probably would have dived into Python as my next major language, but Groovy is fulfilling my outside-of-the-web-browser scripting needs nicely. I also have significant interest in Scala and Clojure, but I think there are more practical advantages to learning PHP better at this point.

In many ways this is analogous to human languages. Although there may be many appealing features to languages such as Italian, French, Japanese and so forth, I feel that learning Spanish is the most practical "second language" for those of us who are primarily English speakers living in the western United States. There is little question that Spanish is the most common spoken and written language encountered on a daily basis in the western United States after English. Even if I was attracted to languages other than Spanish, the most practical move would be to improve my Spanish vocabulary and speaking and listening skills. It's the language that I'd have the most opportunity to use and take advantage of on a regular basis. For me, PHP is like this in the computer programming world. I already know several widely prevalent programming languages in Java, C, and C++, but PHP is an obvious "major language" that I am likely to run into repeatedly in the future.

PHP's Fit

PHP is obviously known as web scripting language. The PHP FAQ states, "PHP is an HTML-embedded scripting language." Although PHP does now offer command line scripting support, I'm not interested in it for that. I have a difficult time imagining where I'd use PHP for command line scripting over Groovy, Python, or even Perl. In fact, I'd probably use shell scripting before I'd use PHP command line scripting. It's definitely on the web that PHP offers its greatest potential and benefit.

NetBeans PHP Support

A minor factor influencing my decision to start learning PHP in earnest is the availability of PHP code support in NetBeans 7.0 (available in previous versions as well). I use NetBeans frequently for my Java and Groovy development, so it is nice to continue using a tool I'm comfortable with to work with PHP. Although there is a "PHP only" download option for NetBeans, I already use the "All" version that includes PHP.

Conclusion

Despite a general disdain toward PHP from most of we non-PHP developers, it is obvious that PHP has been wildly successful in the web browser space. I still don't know if I'll learn to love or even like PHP, but I want to believe. I certainly think I can benefit from a certain level of comfort with such a prevalent language. I also believe that basic familiarity with PHP can open up new software development opportunities. Learning Groovy has provided me with several practical advantages that I use on a near daily basis, but some of the new languages I have learned before have offered less concrete advantages for regular use. I'm hoping learning PHP will provide me the types of benefits that learning Groovy provided. I doubt that I'll ever use PHP as much as I use Java/Groovy, but I do look forward to learning more about a language that is largely new to me.

Original posting available at http://marxsoftware.blogspot.com/

Related: