Let’s get it out of the way early: PHP is a strange and ugly language. It’s not exceptionally fast. It’s not beautiful syntactically. It’s not formulated around a clear opinion about good software development practices. And it’s still what I write a lot of software in. The obvious question is: why? Why use PHP today?
There are lots of good reasons for it, above and beyond personal idiosyncratic preferences. Here’s the space to cover exactly that. Why is PHP my language of choice for web development?
What is PHP?
“What is a PHP?” Or, more commonly, “what’s PHP?” In short, PHP is a programming language made for the web, built up from the C programming language, and which uses idiosyncratic HTML-like tags (or sigils) to contain its code. The PHP programming language is mostly used server-side, which means that it runs on your web server software, which is customarily going to serve HTML to your visitors.
PHP initially stood for “Personal Home Page.” Because that pretty thoroughly constrained the meaning and desirability of using the language for general use, the language now stands for “PHP: Hypertext Preprocessor”. This is what’s called a recursive acronym (a name that contains the name). Nerds love them.
What can PHP do?
What is PHP used for? Basically, anything that you want to do on a web server, you can do with PHP. Make a blog? Yep. Create a full fledged software-as-a-serivce application? Absolutely. Write a little script to process some data in a few seconds? PHP is great for that. Write a complicated set of scripts that accidentally becomes a successful software business? PHP is used like that a lot.
If you don’t trust me, the PHP website lists the following uses:
- Server-side scripting
- Command-line scripting
- Writing desktop applications
I’m not so sure I’d encourage the last bullet point, but it is possible. But the first two are common and good reasons to use PHP in 2018.
This leads to one important and unavoidable fact…
PHP is EVERYWHERE
There are a lot of reasons to know and love PHP, probably the most potent and valid of which is this: it’s used and runs EVERYWHERE the web does. Your cheap little $3 per month hosting account may let you run a web application in Python or Ruby if you shop carefully. But it’ll definitely run PHP. This means that you can count on it wherever you are.
And because it runs everywhere, and is easy to get started with, a lot of very popular software is written in PHP. WordPress is the example that’s both largest and most familiar to me, but tools like Joomla, Drupal, Magento, ExpressionEngine, vBulletin (yep, that’s still around), MediaWiki, and more are all running PHP on the server.
WordPress proudly boasts that it powers more than 30% of the internet. You don’t need to even trust that fact to realize that a lot of the internet must be using PHP if that fact is even conceivably true.
PHP has some very good qualities
Easy Dynamism is Baked into PHP
PHP does HTML rendering and programming easier than almost any other language. So it’s pretty simple to change HTML to PHP. Just change your
file.php, add a bit of dynamism inside some
?> tags. Most web servers will have already been configured to take care of the rest for you. It’s so simple that almost anyone can get started without a need for much more of an understanding of programming than this. Because PHP is so friendly to cut your teeth with, a lot of bad code is written in it.
Once you realize that most bad PHP is written by novice programmers, most of the fear and hatred of PHP you encounter in the world comes from one other essential problem: PHP has never had a grand design with a visionary idea about why it was the perfect language for your web server. Instead, its the result of wide collaboration through an open process which serve as a crucible through which any good idea — and a few bad ones — must pass.
Object-Orientation with (Great) Package Management is now the Norm in PHP
And bad ideas can make it through that crucible. The most popular example is that PHP 5.3 — separately widely regarded as the first modern version of PHP — introduced the
goto statement, which is generally either scoffed at or thought an easy source for errors.
Similarly bad thing in PHP that have resulted from the process through which the language has grown: object-orientation was first implemented as a flawed and limited concept, the standard library is full of inconsistent names and parameter ordering, and (in an example that recently got a fair amount of attention) the
:: token is called by the interpreter by the inscrutable-to-English-speakers:
But today, OOP is fully-realized in PHP. Few languages have as much Java-like OOP practice than PHP. What’s more, unlike Java, PHP has a single and widely-love package manager, called Composer. It was very good, and so the ease of pulling in other well-written and well-maintained libraries in PHP is nothing to be trifled with.
PHP Has Gotten a Lot Faster
But those thing said, PHP is evolving in interesting ways. It’s growing toward being a pretty fully-featured Java-like (for better or worse) object-oriented language. And much like Java, it’s gaining easy abstractions for functional programming — arguably the current hotness. It’s also growing a pretty awesome set of tools — PHP loves Composer, and for good reason — and a commendable effort to make all of these large open source projects in PHP work a little bit better together.
Oh, and we shouldn’t forget the current hotness: speed gains PHP has made in the PHP 7 series of releases. This is widely regarded as having been initiated by the HHVM coming out of Facebook. For a short time, there was a risk that the speed of the HHVM would fracture the PHP community. But it didn’t. Instead PHP just got so much faster that people have mostly forgotten that the HHVM exists.
PHP has a HUGE Community; it’s Learning-Friendly
If you’re deciding what cool new technology to use, a thing I think often gets to little attention is what’s its like to come to terms with the language. What’s it like to learn PHP? PHP tutorials are common and often quite good.
One downside of PHP’s popularity is that you can find some not-so-good training from people who don’t know enough about what using the tool is really like. Or who last were following “best practices” that were current a decade ago. But on the whole, that’s rare-enough that I don’t think it should discourage you. Finding out a practice you’ve used for a few months isn’t great is rare, and not a huge problem.
Common Comparisons to PHP
A lot of people new to programming are looking for very general face-offs of languages. So, here they come. All the languages I’m going to write up here have the following traits in common with PHP:
- They’re open-source. What this means is that you can use the underlying language for free (no cost), and you are able to see and understand the underlying program if you wish to.
- They’re often used for web development. Pretty straight-forward, these are languages used a lot for web development. Some are also widely used outside of that venue, but not all.
- They’re high-level, loosely- and dynamically-typed. This means that a variable can change types, and that you don’t have to define when you define a variable what type of things (numbers vs strings vs objects, etc) it’ll store. This is generally favored for web programming, but not universally.
- Their communities are good-sized or better. There are a lot of interesting languages that have the qualities listed above, but which don’t have a large community of practice. I’m leaving them aside here.
PHP vs Active Server Pages ((.Net) Core)
Active Server Pages started life as a closed-source Microsoft language for web programming. It was pretty directly comparable to PHP, but ran in Windows Server environments. That was replaced by ASP.net. Which has now been replaced by ASP.NET Core. The latter two are now open-source languages, like PHP is. And I’ve personally never written a lick of ASP, in any variation.
I would favor ASP if I used and loved Microsoft servers. Otherwise, give me PHP please. The size and scale of communities are not really comparable.
Should I Use Ruby or PHP?
Python vs PHP: Which is Better?
The last language that makes sense in a one-to-one comparison with PHP is Python. Python is used in many more contexts than web development (it’s also very popular for statistics and data analytics). It’s also one of the more stable and well-designed languages around, in popular consensus.
PHP vs Go? Scala? Java? etc?
I mentioned at the top that there was a lot of similarities with all prior direct language comparisons I made. But if we drop those constraints, there are tons of other languages we could compare. So to briefly touch on a few:
- Java is hugely popular, and very fast. It’s used for building Android apps, desktop apps, and web apps. It’s not dynamically typed though, which has better performance guarantees, but worse ease of web programming.
- Go is a newer and growing Google-backed language. It’s focused on web servers, but slightly lower-level (more like C) than PHP. It’s fast, but the community of practice is smaller.
- Scala is a popular JVM-running (Java-compatible) language that still seems to be growing in popularity. It’s more elegantly designed than PHP, but I can’t say much else about it with confidence.
Again, that’s all I feel like I’ve got enough context to touch. But there are even more options you can consider. But that’s the end of the specific alternatives I’ll consider to PHP.
Programming Language Choice is About Context
Given those comarisons, why use PHP? I’ve enumerated some of the things that are bad about PHP, and I feel some of them regularly. Needle and haystack inconsistency in search functions bites me at least once a month when I’m quickly doing something without autocomplete.
For a true and complete greenfield project, with no need to interoperate with any other system, or need to run in any arbitrary environment, with no existing expertise on the team, I’d think pretty deeply before I recommended PHP. Python really appeals to me in that scenario. But those are a lot of caveats. There are great reasons for even a fresh greenfield project to be written in PHP.
Any modern web language can help you make good websites and apps. And every one of them has features that aren’t ideal. PHP, like any of them, requires you to truly understand its trade-offs for you in your project to determine whether or not it makes sense for you. But for me, personally, I continue to find that it’s possible to be — as Keith Adams put it in a great talk — “shockingly productive in PHP.”
If you’ve got a team of Python programmers, write your application in Python. If you’ve got a cofounder who knows Java, write your SaaS in that. The personnel considerations of software should always outweigh the languages ones.