Is PHP Really that Awful?
Take a look at features of PHP in its latest versions and you’ll see a pretty modern and forward-thinking language. It has proper type hinting, null safety, generators, attributes (similar to Java Annotations or Python Decorators), union types… It even has coroutines support via an external framework.
On paper it looks on par with other interpreted languages/environments like Python or Node, maybe even stronger… When you check its performance, it is also comparable to Python or Node and ahead of Ruby.
But let’s be honest, PHP’s popularity declines a lot over the past 10 years. Have you heard it being used recently for the new projects that are not based off Drupal or Wordpress? I haven’t even seen a PHP developer under the age of 30 for a long time.
But the most striking thing is PHP’s reputation. To me it feels like PHP is more hated than Harvey Weinstein and Monsanto. The best things I hear about PHP are something like:
It does the job, I tolerate its quirks.
However, more frequently you’d hear following:
PHP is absolutely fucking disgusting!
For comparison, most developers who used Python or Kotlin were really happy with it.
Let’s find out, why there is such a disconnect between PHP’s capabilities and its reputation.
Let’s pretend you’re a heterosexual guy. Imagine you’ve met a girl. Girl who is smart, easy going and funny, she has a nice body… but her face is ugly! I mean really fucking ugly: her nose is huge, her left cheekbone is larger than right one, the holes between her teeth are so large you close your eyes out of disgust when she’s smiling.
Programming PHP is like dating that girl - not for everybody.
PHP is a programming language designed specifically for web development. It has (and always had) terrible design flaws, we’ll discuss them briefly in the next section. Until recently it also had a bad performance and some security issues.
Basically PHP’s standard library is mostly a collection of functions in the global namespace. Those do not follow a single logic, code style or any common standards at all. Well, not just a standard library, but the whole language design is the most inconsistent out of any mainstream programming languages out there.
Despite these flaws, in the late 1990s it became popular and for a decade was a de-facto standard for web development. PHP’s competitors of that era (Java, C/C++, Perl) were more generic, complex and simply less suitable for the web. Moreover, PHP had a very shallow learning curve, which allowed many people with little to no previous programming experience to make actual websites! PHP simply was better for solving most business problems.
Nowadays, over 20 years later, backend/web landscape is extremely competitive. We have few strong languages/platforms, that are as good or even better for web development, while lacking PHP’s inconsistent design.
The team behind PHP recently put a lot of efforts into making PHP more modern and performant, but never fixed its awfully designed standard library - the exact thing, that turns engineers off.
Wanna know the details? Read the longer story then!
If you’re looking for details, there is a brilliant talk by Rasmus Ledorf (PHP’s creator) describing how PHP emerged.
Rasmus was a developer using C for web development and it was quite painful (no good HTML templating tools, need to recompile the app on each change, etc), so he created PHP as a HTML template language, assuming all the business logic would be still written in C/C++.
However, the community fell in love with PHP, since it was easier and faster than anything else on the market. People just started using it as a standalone language (despite it was never intended to). The community built functionality they needed in PHP (most of that functionality was implemented as a procedural bindings to C libraries) and submitted it to Rasmus for inclusion in future PHP versions.
Probably, it was reasonable to prepare strategy for PHP, formalize its coding style, prepare an ideology, development guidelines… but it was just a HTML templating language, remember? So Rasmus just accepted those submissions as is.
Accepting all those submissions was, probably, one of the worst engineering decisions in human history. It allowed PHP to grow very fast, but made it extremely inconsistent, unpredictable, hard to debug and flat out ugly language.
In the standard library you have strpos(), but str_replace() string methods. There is isSet() method for checking if variable is set, is_int() method for checking if variable is an integer and how do you think a method checking whether an array is empty would be called? isEmpty()? is_empty()? No, just empty().
Those are just few examples: global namespace is flooded with thousands of inconsistent functions like that, the order of arguments in them also feels pretty random.
Moreover, years passed and backward-compatibility was preserved, so we ended up with things like three ways of accessing a MySQL database: mysql, mysqli, and the PDO… You must always remember which one to use.
Why the hell should you remember all that?
If you’d like to get into more details on PHP flaws, there is a great article: PHP: a fractal of bad design. The article is old, but most of the issues (except performance ones) are still actual. To describe how quirky and unpredictable PHP is, the article compares it to the hammer with the claw part on both sides.
What is the competition today?⌗
In the 90s, when PHP emerged, its main competitors on web development scene were C/C++, Java and Perl. Nowadays nobody uses C/C++ for usual web development, Perl is dead, Java became much more advanced and is used mostly for larger projects. But besides PHP and Java there are few other important players on the scene: C#/.NET, Python, Ruby, NodeJS, Golang… And that’s just mainstream ones!
Let’s pick server-side web development areas where each of them excel, while others don’t:
- Java, Golang, C# — application performance, code readability;
- Python — development speed, easy to use, lots of libraries;
- Ruby — development speed (especially for RESTful APIs and full-stack apps), expressive language;
- NodeJS — ability to use same language and libraries on frontend and backend;
- PHP — cheap hosting, quick prototyping;
It may sound controversial, but I believe the only areas where PHP is far better than competitors now are cheap hosting and ability to code quick and dirty prototypes. It does not mean everything else is terrible in PHP (the only terrible thing there is a standard library’s design and everything related), but competitors have everything else as good or better.
Cheap hosting was important 10-20 years ago, when creating content website or even photo gallery assumed hosting your CMS somewhere and as a consumer you obviously wanted hosting to be cheap. But today consumer-level things like that are almost always handled by SAAS solutions. You don’t need need to host a CMS anymore.
So despite cheap hosting is a great advantage, very few projects need it today.
Quick prototyping is probably a very small niche, especially since building something big out of those prototypes will force you to encounter infamous PHP design flaws…
Should I use it nowadays?⌗
If you’re already familiar with PHP, nothing wrong in using it for your hobby projects, etc. But for commercial ones there are many better alternatives now.
Nonetheless, Wordpress and Drupal-based websites are still a huge niche.
What is the future of PHP?⌗
PHP is in a pretty unique situation. Currently it is an outsider with a bad reputation. However, its authors still could create a new beautiful standard library for PHP and invest in marketing to clean up the reputation. With its stability, functionality and available tooling PHP could easily get back into the mainstream.
Otherwise it’ll just become a grandpa language of the Wordpress generation… Like Cobol today. Anyway it won’t die anytime soon.
Maybe you’re a PHP developer, reading this article and thinking I’m being too mean with PHP. I have a better thing for you to do - please name at least one mainstream language uglier than PHP!