Saturday, April 16, 2011

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.

15 comments:

Dustin said...

From my very limited experience reading PHP blogs in recent days, I have definitely been left with the impression that there is more overlap and associated tension (comparisons/contrasts/flame wars) between PHP and Ruby on Rails than there is between PHP and Java. This makes some sense because PHP and Rails are largely aimed at the same web-only crowd whereas Java has a much broader purview. A good example of this is the recent post Confessions of a Converted PHP Developer: Ugly Code. I have seen the positive influence of Ruby on Rails and Ruby on Java, but it appears to me that the effect of Ruby on Rails may have been even greater on PHP. Indeed, the effects of Ruby on Rails are likely as much to credit for the improvement of PHP development practices as anything else.

balor123 said...

I share the same impression of PHP. I think its popular because small learning curve. I wrote my first web app in PHP for this reason. But in retrospect, it really is a great way to write slow difficult to maintain software for a very small team. Groovy really is great though and I'd pick Groovy/Grails over PHP easily today.

ta.maximus said...

Thanks for the post.

I used to hate Java for the exact opposite reason you hated PHP back 10 years ago. I found Java too academic, too restrictive, a steep learning curve, not only slow in development but also buggy and slow in execution (at least the first few JVM's). I hated the verbosity of the language, the lack of community support and so on. But that was a long time ago and I feel different about Java today for various reasons.

I did however stick with PHP, writing multiple commercially successful web applications and in general I can say that I am really glad I did.

You also mentioned Python. About a year ago I also started working with Python since I needed to develop some non web based applications and even there one can sometimes pick up the "hackiness" one tend to get in PHP scripts. But bloody hell, it is a super productive language and productivity is a measure I take above everything else. I am now an official Python addict.

Dustin said...

I just added the section "NetBeans PHP Support" to the blog post above. I had originally planned to include it and forgot to do so before submitting the post.

It is also worth noting that the book Programming Amazon Web Services uses Ruby for its examples, Amazon Cloud Computing with Java is obviously Java-focused, and Programming Amazon EC2 provides examples in PHP, Ruby, and Java.

Dustin said...

balor123 and ta.maximus,

Thanks for your responses. I agree with your sentiments in general and think that they are further evidence of the value of being willing to learn and use more than one programming language.

Akash said...

Nice post.. I started learning PHP recently till then I had the feeling that PHP is not for Java developers. But once I started learning it became really interesting...

Dustin said...

Akash,

I'm definitely hoping for a more positive experience with PHP this time as I give it more of a chance. I think improvements in PHP along with my own willingness to find things to like in different languages and my experience with dynamic languages Groovy and Ruby may help me "appreciate" PHP more now.

Dustin

Destino said...

Hi Dustin, if you wanna see a good framework in PHP, I would recommend the Zend framework. It's simple an very extensible, including a little queue manager. Very nice.

Dustin said...

Destino,

Thanks for the comment. I've read many good things about the Zend framework. I won't really be able to appreciate it until I've learned a little more about PHP, but I appreciate the recommendation and plan to look at it further when I can better understand what it offers.

Dustin

Dustin said...

In Expanding your toolbox - Picking a new language to learn., Stephen talks about picking a new programming language to learn and ends the post with, "If in doubt just learn PHP to be marketable :)"

Dustin

Dustin said...

I've recently run across some posts with some good, bad, and ugly of PHP: Defend PHP, Ten PHP Best Practices Tips that will get you a job, and PHP Sadness.

Dustin

User said...

I see you found my "ugly php" rant on LeesWeblog. I just went back to read it again, and I still stand behind what I wrote...which is amazing, to me.

The landscape of PHP developers is changing. There is still a wealth of crap-code out there, but it's getting better. I employ and work with 6 people who use PHP for some really big problems...and we do it well! This isn't something I think I could have said about the PHP-centric-industry in general, even 3 years ago.

Dustin said...

Lee ("User"),

Thanks for providing that feedback. Although I generally have felt that the PHP community has improved significantly, I at the same time realized that I am an outsider to PHP. It is nice to hear from folks who are more familiar with the PHP community.

Dustin

Dustin said...

What I Love about PHP is another blog post from a different blogger about his experience transitioning into PHP development.

Dustin said...

C├ędric Beust has posted I like PHP.