5 Strengths of PHP

May 14 2014

PHP. It's the much-maligned whipping boy of "serious" programmers. And in some sense, its ill repute is justified. PHP's early evolution was choppy. The standard libraries don't feel all that cohesive. Decades ago, it had some security issues. And the community, as diverse as it is, continues to churn out just as much bad PHP as good PHP.

However, PHP does have strong points. While I have developed vast phalanxes of PHP code in the past, right now I am working in several different languages. And every once in a while, I find myself missing PHP. I distilled my list of things I miss into five distinct things. And here they are.

1. One Data Structure To Rule Them All

Surprised that I led off with the feature I gripe about? Me too. I like having a broad collections library, and it still irks me that PHP "arrays" are really ordered hash tables. But... I have to admit this much:

If I were asked to create one data structure for a programming language, and told to make it simple to use, highly flexible, and relatively quick performance-wise, I would point to PHP's arrays.

The purist can gripe about the ugly internals of this sort of data structure, and quite frankly I would agree... except that PHP arrays are just so darn useful.

2. Web-first

W3 Techs just put out their May 2014 report, and unsurprisingly they point out that the web continues to run on PHP:

PHP is used by 82.0% of all the websites whose server-side programming language we know.

(Yes, yes... you can attach all the significance that you want to that last phrase. My Go servers certainly don't advertise that they're Go. But still... we're talking a huge percentage of sites.)

Why the crazy high success? Surely there are multiple reasons, but one of them is the fact that PHP was built as a web language. No need to do any web bootstrapping in your PHP code. From your very first line of code you can assume that you're running in a web server.

Detractors may find this claim inconsequential. "So what? You can start with framework X in language Y and be at the same place!" What is overlooked in this claim, though, is the cognitive workload (often accompanied by hours of configuration) necessary to "start with X".

3. Vast Troves of Documentation

Too often, languages fail to gain market share not because of their inherent limitations, but because they are hard to learn. And they are hard to learn because there is not much documentation.

PHP surely does not fall into that camp. PHP.net has been the go-to source of documentation for almost as long as PHP's been around. And the documentation there is largely very good. (There are exceptions, of course.) But that's not all. For a decade at least there have been a plethora of blogs, tutorials, video series, books, and training classes dedicated to PHP.

PHP may be a language for the hoi polloi, but it's documented like one of those stodgy old Enterprise languages. You can Teach Dummies the Good Parts of PHP in 24 Hours, in a Nutshell (TM).

4. Surprisingly Good Standard Libraries

Okay, admittedly, "good" is a relative term here. There's "good" in terms of consistency. PHP's standard library is certainly not that (needle and haystack swap, anyone?)

There's "good" in terms of complete. PHP may or may not hit this one. It's missing some obvious ones (like UUID generation), but has most of the basics more than adequately covered.

But there's also "good" in terms of practicality. PHP is a weakly typed scripting language. That means its got performance penalties coming out of the gate. One way of fighting the script interpretation performance challenge is by optimizing the compilation and interpretation phase. PHP has done this at least in part.

Yet there is an overlooked strategy that many scripting languages don't bother with, but that PHP handles effectively: Provide workhorse functions that perform expensive operations at a low level without as much context switching.

Array manipulation and traversal is a great example. Looping through an array with for loops can be expensive. Using an array_walk function often can accomplish the same thing much faster. Then there's array_diff, array_merge, array_reduce, array_combine, array_unique and so on.

PHP shines in having provided a wide variety of manipulation and traversal functions for frequent string and array operations. And by offloading these workloads to lower-level (e.g. implemented in C) code, things go faster.

5. Stability In The Strangest Form

Here's a major downside of all PHP programs: They're single-threaded. And for the most part, a PHP script is also bound to a single process. (You can get around that one, but it takes a lot of work.)

That stinks.



Except that...

In the most bizarre way, it actually accomplishes something that makes PHP a highly popular language for web development.

PHP doesn't crash.

That's a high-level (and sorta inaccurate) statement, so let me clarify. Let's start by assuming that we're running PHP's most common configuration: mod_php in Apache. Each time a request comes in for a PHP script, Apache executes the PHP script afresh. Other than perhaps re-using the intermediate "compiled" op codes, Apache doesn't re-use anything from previous PHP invocations. (There are a few exceptions to this, but they're not commonly used language features.)

So each request for a PHP page involves creating a separate invocation of the script. No threads. No code level race conditions. No shared objects. No leakage of data across requests.

And what happens when this particular script invocation hits a fatal error? It exits. And while the one user who caused this error notices, nobody else does. Because they're all running separate instances. And when that user sends a next request, a new instance is started. Life goes on.

It's really, really hard to get one PHP script's failure to take down the entire site.

And so, out of PHP's limitation comes this strange virtue: PHP appears to be highly stable.

But, But, But...

So PHP has its virtues. Most of these virtues point toward the claim that PHP is a great language for beginners. That's probably true. I know dozens upon dozens of developers who learn PHP as a first language.

Another conclusion to be drawn from these five is that PHP is a language that makes development faster than other languages. Also true.

It would be a huge mistake, though, to draw the conclusion that PHP is in fact "the best" language, even the best language for web development. While I miss features of PHP sometimes, I am quite pleased to not deal with it on a daily basis. It's nice to be able to write sophisticated and robust multi-threaded code. It's nice to take advantage of persistent connections. It's nice to have strong typing so that the compiler can catch issues well before I push anything onto the server. In short, many of the things that PHP is missing (and will never get) make my daily non-PHP life a happier life.

Be this as it may, PHP doesn't deserve the disdain it gets. It's a decent language with a strong community, broad adoption, and some real strengths.