Node.js vs PHP: An epic battle for developer mindshare
- 24 April, 2019 07:00
But hype fades and software can live on for decades. Most of the PHP code base isn’t going to migrate and it continues to serve up large portions of the text we read each day. By some estimates, 40 percent of the pages we view begin, in some form, with PHP. Part of this is because PHP continues to be reborn. In the last few years, the guts of the systems running PHP have been completely rewritten. It’s not the same PHP code that ran your grandparent’s website.
PHP’s zippy, just-in-time compiler is delivering answers faster than ever thanks to the same smart techniques that powered the Node.js revolution. Now PHP 7.2 and the HHVM offer many of the same clever on-the-fly optimizations that V8 brought to Chrome and Node.js. Not only that, but HHVM has Hack, a clever PHP dialect that offers full support for sophisticated programming features such as lambdas, generics, and collections. So if you need these features, you don’t need to search for a more full-featured stack.
Where PHP wins: Mixing code with content
You’re typing along, pouring thoughts into text for your website, and you want to add a branch to the process, a little if-then statement to make it look pretty, say, depending on some parameter in the URL. Or maybe you want to mix in text or data from a database. With PHP, you open up the magic PHP tags and start writing code within seconds. No need for templates—everything is a template! No need for extra files or elaborate architectures, only programmable logistical power at your fingertips.
Where Node wins: Separating concerns
Mixing code with content is a crutch that can end up crippling you. Sure, it’s fun to mix code in with HTML the first two or three times you do it. But soon your code base becomes a tangled mess of logic. Real programmers add structure and separate the cosmetic layer from the logical layer. It’s cleaner for new programmers to understand and easier to maintain. The frameworks running on Node.js are built by programmers who know that life is better when the model, view, and controller are separate.
Where PHP wins: Deep code base
The web is filled with PHP code. The most popular platforms for building websites (WordPress, Drupal, Joomla) are written in PHP. Not only are the platforms open source, but so are most of their plugins. There’s PHP code everywhere, and it’s waiting for you to download, modify, and use for your needs.
Where Node wins: More modern features
Sure, there are thousands of great open source PHP files, but some are 12-year-old WordPress plug-ins hoping and praying that someone will download them. For every modern version of Symfony, there is a dusty, long-forgotten library that no one updates.
Who wants to spend hours, days, or weeks monkeying with code that hasn’t been updated in years? Node.js plug-ins are not only newer, they were built with full knowledge of the latest architectural approaches. They were built by programmers who understand that modern web apps should push most of the intelligence to the client.
Where PHP wins: Simplicity (sort of)
There’s not much to PHP: a few variables and basic functions for juggling strings and numbers. It’s a thin layer that doesn’t do much except move the data from port 80 to the database and back. That’s what it’s supposed to do. A modern database is a magical tool, and it makes sense to leave the heavy lifting to it. PHP is the right amount of complexity for a job that’s not supposed to be complex.
Then again, if you’re a programmer who wants to do more than interact with a database and format the results, you can now do more with PHP without holding your nose. Facebook’s HHVM adds support for Hack, a complete language filled with modern features like type annotations, generics, and lambda expressions. Using this limits your code to running only on the HHVM, but that’s not the worst thing in the world. It’s very fast.
Where Node wins: Dozens of language options
Where PHP wins: No client app needed
Where Node wins: Service calls are thinner than HTML-fat PHP calls
Where PHP wins: SQL
PHP was built to co-exist with MySQL and its many variants, like MariaDB. If MySQL isn’t exactly right, there are other great SQL databases from Oracle and Microsoft. Your code can switch with a few changes to your queries. The vast SQL world doesn’t end at its borders. Some of the most stable, well-developed code will interface with an SQL database, meaning all that power can also be easily integrated into a PHP project. It may not be one perfect, happy family, but it’s a big one. Not only that, but the database world is slowly getting better as developers find ways to add more intelligence to the database so you don’t need to work as hard.
Where Node.js wins: JSON
Where PHP wins: Coding speed
For most developers, writing PHP for web apps feels faster: no compilers, no deployment, no JAR files or preprocessors—simply your favorite editor and some PHP files in a directory. Your mileage will vary, but when it comes to banging a project together quickly, PHP is a good tool to use.
Where Node.js wins: Application speed
Where PHP wins: Competition
The battle for the hearts and minds of PHP developers is still unfolding. The HHVM team and the Zend team are working hard to deliver fast code for everyone. Independent benchmarks are appearing, and everyone is pushing the code bases to the limit. This only means better performance.
Where Node.js wins: Solidarity
Do you really want two different code bases? Sure, competition helps, but fragmentation soon follows. What happens when your code runs on only one of the two? Competition doesn’t do any good if you have to spend weeks or months rewriting your code. While Node.js experienced its own splintering a few years back, with the launch of io.js, the Node.js universe has since reunited, giving it the kind of language solidarity that PHP developers may soon long for.
Where PHP wins: Basic apps
The backlash against this rococco insanity can be found in the teams building static site generators (463 at this writing) and stripped-down webpages in the AMP format. PHP is a natural choice for any team that wants to concentrate the intelligence on the server so the client isn’t overburdened.
Where Node.js wins: Richness
Where both win: Headless
This may not make sense for someone starting from scratch, but if you’ve relied upon PHP for years and you want to move forward gradually, this can be a happy compromise.
Where both win: Microservices and serverless