FYI.

This story is over 5 years old.

Tech

Know Your Language: PHP Lurches On

Meet the broken monster at the heart of the dynamic web.
Image: Vaughan Leiberum/Flickr (photo only)

Presumably there are people that think the PHP language is awesome. An afternoon spent writing PHP code is like a fine meal and a backrub in one transcendent coding experience while JavaScript and client-side scripting can just go to hell.

They must exist, right? After all, the language and its interpreter, the Zend Engine, are actively developed by volunteers. PHP 7, the latest complete PHP overhaul, was released just two weeks ago (Dec. 5) and at the very least the language's creators must be excited about that, if no one else. Maybe they even threw whole PHP parties.

Advertisement

It's not that PHP doesn't deserve a party. It's in some very large part responsible for the web we experience and take for granted today, which is the web of interactive web pages and web apps. It wasn't always like this and not even all that long ago the internet tended to look a lot more like a collection of billboards and widgets than the collection of experiences we're more familiar with.

Web pages nowadays do stuff in ways more and more resembling software and to get to this point it took a suitable web scripting language. For HTML to interact with users there needs to be something there to change the HTML and that's what PHP does. PHP is a server-side web scripting language, which means that it lives in the same place as all of the other website junk while functioning dynamically to assemble all of that junk in useful ways.

Let's see what that means.

Hello, World

If I wanted to make a webpage with one single function of telling users "hello, world" it would be pretty easy to do in HTML. All it would really take is sticking the words "hello, world" in a document inside a pair of tags. Every time you loaded up my URL, a server would send you the same page with the same words and it would look the same every time, at least when viewed with the same browser.

Maybe that's cool, but maybe you'd like to be able to tell returning users something different, like "hello, again." In this case, I could write a little bit of PHP code telling the web page to only show you "hello, world" if the page had not registered your visit in a text document it maintains also on the server. If your IP address is already logged on that document, the page would say "hello, again."

Advertisement

This is something that HTML can't do. HTML is a markup language and really only tells a browser how things are supposed to look rather than what they're supposed to do.

A fundamental feature of what we usually think of as programming are conditional statements, e.g. if some condition is met then do something. HTML has none of these, nor does it have variables or most anything else needed to turn logic into functionality. It's most accurate to not even refer to HTML as a programming language at all. Again: a markup language.

When we need the dynamism of a conventional programming language we very often turn to PHP. Here we can have conditional statements (and beyond) because the whole purpose of the language is to change things. In PHP we have variables and data types and many of the trappings of object-oriented programming. As such, we can expect that PHP is capable of mirroring the functionality of any other program written in any other programming language, however inefficiently and-or clumsily.

So, for our example, I would write a PHP script that uses a conditional statement to test for whether or not your IP address has been logged. If it has been, the script will do one thing. Otherwise, it will do another thing. This script will look not unlike the C programming language, from which the syntax and structure of PHP draws heavily.

How PHP works

PHP is a scripting language, not a programming language. While the distinction can get pretty muddy, it's crucial for the nuts and bolts of web development. Unlike something like C++, which eventually gets assembled into object code and machine instructions in a step known as compilation, all of the PHP pieces are already there pre-built, in a sense.

These pieces all live in something called the Zend Engine, which is an open-source project sponsored in part by the private for-profit Zend Enterprises.

Advertisement

The Zend Engine is the actual computer program that lives on a server and interprets PHP scripts. As in other interpreted languages (Perl, Python, JavaScript, and others), this sort of blobby entity has all of the needed machine code and memory allocations ready at hand for whatever a script may demand and it's just a matter of arranging all the pre-built stuff in just the right way (again: in a sense).

PHP doesn't communicate or interface with users or the web browser directly: it only generates HTML. It's sort of like a puppetmaster living in the background of interactive web pages. If, say, you enter some data onto a nice looking web form, it's probably PHP that actually grabs your input and does something with it.

The accidental language

The history of PHP is short.

It originated in the mid-1990s as "Personal Home Page Tools (PHP Tools) version 1.0." A lot of what modern PHP is was present then, but its development until 2014, when the language's first formal specification was released, was sort of a mess. An oft-repeated quote from Rasmus Lerdorf, the language's founder, is as follows: "I don't know how to stop it, there was never any intent to write a programming language […] I have absolutely no idea how to write a programming language, I just kept adding the next logical step on the way."

Is this an elephant preparing to poop? Image: php.net

Developers hate PHP

Even the blog post I sourced the above Lerdorf quote from, which is ostensibly a PHP defense from a Zend-certified engineer named Felipe Riberio, concludes with saying that PHP has pushed itself into eventual obsolescence.

"In a very personal opinion," Ribeiro writes, "I believe PHP is a game changer in the history of the Web as it changed the way we develop software in this era and the whole paradigm of this industry. But with the poor decisions and indifference to the 'state of the art' displayed by the core developers, PHP is now behind the others in many aspects and honestly, at least for me, it's no longer my personal language of choice if I would start a new project or my own company/startup."

Advertisement

It's not hard to find literature about "why PHP sucks," but one of the key issues is inconsistency, both internally and with respect to other languages. It's unpredictable and inelegant. It's like slang, but without a formal, "proper" language underneath. Or a toolbox filled with esoteric and half-broken tools that will still be able to fix most things, but only in the most difficult and-or unpredictable manner possible.

PHP is also incredibly insecure.

Further reading:

Coding Horror: "Is PHP so broken as to be unworkable? No. Clearly not. The great crime of PHP is its utter banality. Its continued propularity is living proof that quality is irrelevant; cheap and popular and everywhere always wins. PHP is the Nickelback of programming languages. And, yes, out of frustration with the status quo I may have recently referred to Rasmus Lerdorf, the father of PHP, as history's greatest monster. I've told myself a million times to stop exaggerating."

Fuzzy Notepad, "PHP is a fractal of bad design": "PHP is an embarrassment, a blight upon my craft. It's so broken, but so lauded by every empowered amateur who's yet to learn anything else, as to be maddening. It has paltry few redeeming qualities and I would prefer to forget it exists at all."

Just Google "PHP sucks."

PHP is unavoidable

There are alternatives to PHP—languages that do the same stuff, usually better. They include Python, Ruby on Rails, and Perl. Worth noting is that PHP actually began as a set of Perl programs ("Perl hacks") and it shares much of the syntax. PHP, however, is designed specifically for the web, while Perl is general-purpose.

There is also client-side scripting, another way of generating dynamic web pages, but one that depends on the user's browser rather than a program on a server. JavaScript is an example of this (the example really).

But PHP is still everywhere and holds an 80 percent market-share for server-side scripting languages. Overall, it's still the sixth most popular programming language. I and every other programmer can hate it until we catch fire, but PHP will persist because it's completely saturated the internet. And it's accomplished this simply by being there when the internet needed something like it to evolve: a language to make HTML do stuff. Unfortunately, PHP may now be in over its head.

Read more Know Your Language.