Grok all the things

grok (v): to understand (something) intuitively.

PHP

🙄  Cynics & grumps

Oh, PHP. The language that somehow, despite its flaws and the relentless mockery thrown its way, just refuses to fade into irrelevance. PHP has been around since the mid-90s and still powers a significant portion of the web, including that little upstart called WordPress. Let's delve, with a perfectly straight face, into the world of PHP and attempt to understand why it continues to thrive in an ecosystem where newer, shinier languages are just begging to be adopted.

A Brief, Reluctant History

PHP was initially created in 1994 by Rasmus Lerdorf as a collection of Perl scripts for tracking visits to his online resume. By 1997, PHP had evolved into an actual server-side scripting language called "PHP/FI" or "PHP: Hypertext Preprocessor." Because who doesn't want to name their language as a recursive acronym, right? It kept evolving over the years, with PHP 3 released in 1998 and PHP 4 hitting the scene in May 2000. PHP 5 debuted in July 2004, bringing with it much-needed object-oriented programming support – because before that, PHP was mostly procedural and trying to write any remotely complex script felt like assembling IKEA furniture without the instruction manual.

Fast forward to today, and we have PHP 7.x and even version 8.0 available. One has to give credit where it's due: PHP does manage to keep itself up-to-date-ish (ignoring the somewhat lackluster support for modern programming concepts that other languages have wholeheartedly embraced).

One Language to Rule Them All (And In The Web Server Bind Them)

PHP's continued popularity can be attributed largely to its ubiquity in web servers around the world. Thanks to the LAMP stack (Linux, Apache, MySQL, and PHP), and the fact that it's just so darn easy to get a basic PHP script up and running, there is no shortage of web developers who have at least dabbled in PHP at some point in their careers. When you have a programming language that is practically installed by default on every web server, it's no wonder that people can't help but use it – especially when you consider the minimal barrier to entry.

Death by a Thousand $ Signs

As a language, PHP doesn't have the best reputation for elegance, readability, or even logic. Witness the myriad of function names that seem to have been decided on a whim: strpos(), str_replace(), and substr() mixed with array_push(), array_pop(), and implode(). One starts to wonder if they generated the function names by throwing darts at a wall covered with random prefixes and suffixes.

And who could forget PHP's obsession with the $ symbol? While other languages strive for clean and easy-to-read syntax, PHP seems to think that dollar signs make code oh-so-fancy:

$var = 42;
$anotherVar = $var + 10;
echo $anotherVar; // 52, in case you're wondering

Objects? We Have Those... Sort Of

With PHP 5, the language made a halfhearted attempt to embrace object-oriented programming. Classes and objects were added to the mix, but PHP still clings to its procedural roots like an insecure toddler holding onto their favorite blanket:

class Foo {
    public function doSomething($a, $b) {
        return $a + $b;
    }
}

$fooObject = new Foo();
$result = $fooObject->doSomething(42, 10);
echo $result; // Again, 52

While PHP now boasts the usual array of OOP features – methods, inheritance, and interfaces – it can't quite shake the feeling that its heart really isn't in it.

PHP: The Cockroach of Programming Languages

PHP is a language that, despite its quirks and flaws, continues to power millions of websites around the globe. It can be easy to sneer at PHP's shortcomings (and there are plenty of those to be found), but one has to admit that it gets the job done. If you're looking to build a quick and dirty script, crank out a WordPress theme, or just want to indulge in some masochistic nostalgia, PHP might just be the language for you.

Sure, other languages are more elegant, more powerful, and arguably more enjoyable to work with. But if you're stuck maintaining a legacy application or simply feeling like reliving the early days of the internet, PHP will always be there, waiting with open arms... or at least a messy pile of dollar signs and inconsistent function names.

Grok.foo is a collection of articles on a variety of technology and programming articles assembled by James Padolsey. Enjoy! And please share! And if you feel like you can donate here so I can create more free content for you.