PHP: A Quick Introduction to a Complex Language

PHP is a quarter-century old… and for the first 20 years, it had no written specification. That’s astounding, and probably explains why it was a bit of a challenge for many developers to get their heads around (one critic famously described the language as a fractal of bad design).

Nonetheless, PHP, which is used largely in web development, proved compelling enough for developers to stick with it—and they did, using it to create any number of interesting products and tools. There are more than 450 PHP-based apps listed on Softaculous, which is an open-source installer that comes with many website hosting accounts. You might have heard of a few of these fine products: WordPress, Drupal, Laravel, and many, many more!

Over the past few decades, PHP has also evolved. In fact, you could argue that the current iteration, 7.0, is where the programming language truly reaches its maturity. Let’s dive in.

What’s New in PHP?

PHP 7.0 made its first appearance in late 2015. It succeeded 5.6; 6.x, which would have provided native Unicode support, was never released (performance issues related to UTF-16 conversion reportedly killed the project, along with a lack of developers). By the start of 2019, only 7.x versions had official support, although I bet there are a load of pre-7 versions still in use (I’m running PHP 5.6.40 on one machine, for instance, and Debian is providing support for PHP 5.6 until June 2020.)

Version 7.0 introduced many advances to the language and its internal structures, resulting in considerable speed improvement (100 percent, in some benchmarks). I particularly like intdiv, which makes integer division safer and features the the null coalescing operator ?? (as in C#). If you are used to using lots of isset to check if a variable exists, 7.x cleans up the syntax (and that’s great!). Here’s an example from the PHP 7.0 migration guide:

$username = $_GET['user'] ?? 'nobody';  // new way
// Equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody'; // old way

I reckon the best feature of 7.x is return type declarations, further enhanced in 7.1 by making them (and parameters) nullable. This is very much like C#, as is the types of return typing like this:

function arraysSum(array ...$arrays): array

Be sure to use strict type enforcement (from PHP 7.1). Just put this line at the top of the page:

?php declare(strict_types=1);?

The developers have moved releases to a consistent three-year supported lifetime. PHP 7.0 went out of support at the end of 2018, with 7.1 reportedly doing the same at the end of this year.

Package Support

PHP has enjoyed package support for some years with PEAR, officially part of PHP. And 2012 saw the release of Composer, a package manager that manages dependencies using sophisticated SAT solving (a mathematical technique). The official Composer package website packagist.org has almost a quarter-million packages (and over 1.678 million versions).

While this is a bit off-topic, I think it gives a lot of credibility when a programming language has a robust repository. It certainly beats searching through innumerable download websites for whatever you need!

The Downside

If you have a website with PHP 5.x and you moved to PHP 7.x, there’s a fair chance that something might break. You can’t just change the PHP and guarantee it will run. If this has happened to you, it’s worth creating this small script in order to see what’s missing:

 
?php
phpinfo();
? 

The phpinfo() function outputs more than 700 lines of text—a mass of information about the modules and features in your PHP installation. Just be careful not to make it publicly available, which would give hackers a jump-start on hacking your site!

Also, hosting companies sometimes don’t realize they’ve missed out modules and broken websites. My own website used a feature to run PHP in .HTML files, and the provider (staymommy or something like it) inadvertently broke it, so instead of running the PHP, it listed it.

Conclusion

PHP has one major advantage: It comes with every Linux system, and also runs on Windows web servers. That means it’s likely to stay in the web-building ecosystem for quite some time (right now, it’s also ranked eighth on the TIOBE Index, signalling a good deal of developer interest).

According to Netcraft’s monthly web survey, the language’s presence is split mostly between Nginx web servers (29 percent), Apache (29 percent) and Microsoft (19 percent). I wasn’t not sure if the oft-quoted figure of “80 percent of all sites run PHP” was an accurate one, but with a bit of digging, I found a figure of 79 percent (on W3Techs). That’s pretty big!

Most of the rest are likely to be IIS servers running ASP.NET MVC, the main alternative to PHP. There are a few others, as well, but ultimately it’s a two-horse race.

PHP is easier to learn than the C# powered ASP.NET, which comes with a bit of a learning curve. That being said, there’s probably not a lot of difference in execution time between the two. I’ve timed PHP pages including pulling data from a MySQL database, and it typically takes 1-2 ms to generate the page. That is nothing compared to the few seconds it takes to download and render the page in a browser.

I’d gone off PHP programming a few years back… but I’m now quite tempted to get back into it.