This article was sponsored by AppDynamics. Thank you for supporting the sponsors who make SitePoint possible!
It’s a popular question. Why indeed should one pick PHP over one of the alternatives? After all, PHP has often been dismissed as a hacky, unusable and badly designed language. Why would anyone choose it, when starting a project from scratch?
Instead of listing the reasons why people do choose it (mostly widespread availability), let’s instead focus on why people should choose it. We can’t talk about that, however, without first mentioning why they shouldn’t.
When you should NOT choose PHP
When considering command line apps
If you’re building a command line app, PHP is not the right choice. Sure, there are decent approaches to building CLA with PHP, but it simply isn’t intended for that. PHP is a web language, first and foremost, and a pure command line app is better built in something else. That’s not to say it isn’t possible – but it’s definitely quicker and easier to do it in another language – not to mention more performant. Building CLI apps in PHP lends itself to the metaphors people use when comparing PHP to a double-clawed hammer. It just isn’t a tool that’s intended for it. For example – Python is pre-installed on most *nix distributions, so you have immediate access to it without needing
php.ini hackery, and can start working right away.
When it seems easiest
If PHP seems the easiest of the bunch and that’s your sole motivator, your heart is in the wrong place. PHP can get complicated fast, and while the entry path is, admittedly, easier than with other languages, the pro level shares its complexity with alternatives. Doing serious work is similarly difficult in any language.
Just because your shared hosting provider supports it
If you’re using a shared host, and delight in the fact that it supports PHP (maybe even a not totally archaic version like 5.3), stop. If you’re calling yourself a developer, you should never use shared hosts for anything except the banalities like custom webmail, domain management, or very simple demos. With alternatives such as DigitalOcean offering virtual private servers hosted on SSDs for as little as $5 per month, picking a shared host makes no sense – not when a private instance gives you so much more control. If you’ve got something against DevOps and don’t like setting up your own server (you should really learn to do it, though), many, many cloud hosting providers offer free tiers that are pre-configured for you. Stay away from shared hosts.
Computationally intensive software
When you expect to be dealing with a lot of math, heavy calculation, statistics and the like, there are better options – especially if the app does mostly this, and little of anything else. In particular, functional languages like Scala or the newcomer Dart will perform much better at this task than PHP, which might require more effort than it’s worth to get it to do what you want. It’s also significantly slower at these tasks than languages specifically designed for them.
Sometimes it’s simpler to write a computationally heavy part of the application in a language you’re already involved with in the current app, but more often than not that’s a lazy shortcut. The discomfort of having to install a helper language or VM and build part of the app as a separate application is quite often offset by the long term gains, especially when traffic starts to demand it. There’s a reason Twitter’s back end calculations are all in Scala now even though their frontend is still RoR.
PHP is highly learnable
The problems listed above are problems of inadequate knowledge and experience, and naturally, when someone uses PHP for such a purpose, the end result is suboptimal – adding bad press to PHP’s already tarnished reputation. What PHP is good for, is any web oriented scenario that needs to be developed quickly, and needs to perform really fast.
PHP sports an enormous community. True, the vast majority are newbies, but once you weed out the chaff, what remains is a handful of dedicated professionals like Phil Sturgeon, Josh Lockhart, Mike van Riel, Anthony Ferrara and many, many more. A dozen of highly dedicated professionals who admit to PHP’s faults but also work hard on educating the public and fixing these issues can provide a wealth of knowledge. Coupled with excellent resources like SitePoint’s forums, StackOverflow and PTRW, problems encountered in PHP are easily solved, and progress is usually both rapid and highly educational. Unless the questions you ask are downright stupid (and yes, there are stupid questions), the community is non-elitist and always ready to help.
The number of open source PHP projects as well as books and courses one can learn from is plentiful, and with patience and guidance one can become highly proficient in the language in a relatively short period of time.
Dispelling the bad press
PHP has matured significantly since the last time people collectively bashed it, and is inheriting more and more modern features from other languages. Prejudice is prevalent, and one should learn to recognize it. As this excellent MailChimp blog post from 2010 says, it’s not the tool, it’s how you use it.
PHP allows rapid prototyping of all types of applications, and can get you up and running in no time. It’s a fast and robust language that outperforms RoR and Python by a lot in complex web apps, and with the added benefits of projects like HHVM and Phalcon, PHP’s performance is unchallenged in the land of dynamic languages. Typing is coming in both regular PHP and HHVM’s Hack in at least some form, and as giants like Facebook continue to work on it directly, PHP can be taken ever more seriously.
Drinking game for web devs:
(1) Think of a noun
(2) Google “<noun>.js”
(3) If a library with that name exists – drink
— Shay Friedman (@ironshay) August 22, 2013
It’s not my intention to bash other languages – other people can do a much, much, much better job at bashing JS than I can – what I’m trying to point out is the fact that one can nitpick on the downsides of absolutely any language. Just like in life, if you focus only on the bad stuff you’ll never experience the good – PHP’s downsides have been taken care of for the most part by libraries and frameworks, and while one may argue that a “real language” doesn’t need patches or the help of big companies to work, one could counter by asking what exactly Java, NPM or PUB libraries and packages are, if not patches and improvements to the language, adding missing functionality someone else was kind enough to build.
So let’s stop focusing on how bad X language is, and start working on projects that demonstrate the superior nature of our choice. Yes, there are projects out there dragging it down – WordPress is one such project for PHP – but if we persist and build awesomeness with best practices in mind, we can dispel this bad reputation and start rebuilding the PHP world we love.
PHP is an excellent choice for new projects regardless of complexity. The PHP world has been evolving at a truly rapid pace in the past few years, and some revolutionary new features can be put to use even today (refer to the HHVM and Phalcon links above). Far be it from me suggesting PHP should be the only tool in your toolbox – being fluent in more than one language is just as useful in programming languages as it is in natural languages – but it definitely will not hurt to master it. PHP will grant you the ability to go from idea, to prototype, to production in days – and that’s a claim not many languages can make. Recent advances like built-in OpCache and virtual machines like HHVM take the language further, allowing you to greatly reduce your cloud hosting bills, and performance monitors like AppDynamics will let you milk it to its limits.
When considering your next project – don’t ask yourself “Who hates PHP?” or “What will people think?”. These are questions of vanity and insecurity. You think anyone assumed MailChimp, the system that powers millions of emails per day, was built on PHP before they “came clean”? Instead, just do your best and build.