PHP vs. Node.js: A titanic battle for developer mindshare

  • back to the house
  • Development of software
  • What are the differences between the old guard and the upstart darling of the server-side web? Let’s compare and contrast.

    Frequently, the enmity begins when one of the pals develops an interest in what has always been the unspoken domain of the other. The introduction of Node.js in the programming language version of this movie turns the buddy flick into a grudge match: PHP and JavaScript, two partners who once ruled the internet together but now compete for the minds of developers. The battle between two old friends who went their separate ways is a classic Hollywood plot.

    People can’t spend more than a minute on the internet without coming across PHP, thanks to sites like WordPress, Drupal, and Facebook. The partnership used to be straightforward. JavaScript took care of minor details in the browser, while PHP took care of everything on the server side between port 80 and MySQL. It was a happy marriage that has continued to support many important aspects of the internet.

    To create Node.js and the frameworks that run on the client, only one language was required. For some, “JavaScript everywhere” became a mantra. There was no longer any need to use PHP to construct the next generation of server stacks. Then some bright kid figured out how to get JavaScript to run on the server.

    JavaScript’s popularity has skyrocketed since its discovery. The list is lengthy, and the most difficult part is deciding between excellent options. Developers using Node.js can now choose from a growing list of excellent frameworks and scaffolding, including React, Vue, Express, Angular, Meteor, and more.

    JavaScript is used in a lot of the coolest projects, and it’s mentioned in a lot of the most popular hashtags. JavaScript is the most popular language in GitHub’s repository collection, and JavaScript’s kissing cousin, TypeScript, is rapidly growing as well. Some see the rise of Node.js as proof that JavaScript is clearly winning, and there’s plenty of evidence to back them up. Meanwhile, PHP has dropped from third to fourth place in this ranking, and it’s likely fallen even further in the number of press releases, product rollouts, and other heavily marketed events.

    However, hype fades over time, and software can last for decades. It’s not the same PHP code that was used to power your grandmother’s website. The majority of the PHP code base will not migrate, and it will continue to serve large portions of the text we read on a daily basis. Part of this is due to the fact that PHP is still being reborn. According to some estimates, PHP is used in some form on 40% of the pages we visit. The guts of PHP-based systems have been completely rewritten in the last few years.

    So, if you require these features, you won’t need to look for a more comprehensive stack. Many of the same clever on-the-fly optimizations that V8 brought to Chrome and Node.js are now available in PHP 7.2 and the HHVM. Thanks to the same smart techniques that propelled the Node.js revolution, PHP’s zippy, just-in-time compiler is delivering answers faster than ever. Furthermore, HHVM includes Hack, a clever PHP dialect that fully supports advanced programming features like lambdas, generics, and collections.

    Will JavaScript dethrone its long-time rival to become the world’s most popular scripting language? Place a second batch of popcorn in the microwave and relax. Will the old codger be able to defeat the server-side newcomer? Every coder gushing about Node.js’ purity and youth, or JavaScript’s simplicity, there’s another who’s content with PHP’s deep code base and long-understood stability. Of course, the conclusion has yet to be written.

    Where PHP shines is when it comes to combining code and content.

    Perhaps you’d like to include text or data from a database. There are no extra files or complex architectures required; all you need is programmable logistical power at your fingertips. You’re typing away at your keyboard, converting your thoughts into text for your website, and you want to add a branch to the process, a simple if-then statement to make it look nice, say, depending on a URL parameter. In PHP, you simply open the magic PHP tags and begin writing code in a matter of seconds. There’s no need for templates because everything is one!

    Separating concerns is where Node comes out on top.

    Mixing code and content is a crutch that can lead to your demise. Sure, the first two or three times you do it, mixing code and HTML is entertaining. It’s simpler to understand and maintain for new programmers. Real programmers add structure and divide the logical and cosmetic layers. However, your code base will quickly become a tangled mess of logic. Node.js frameworks are created by programmers who understand that having a separate model, view, and controller makes life easier.

    Where PHP excels is in its extensive code base.

    The platforms, as well as the majority of their plugins, are all open source. PHP is used to create the most popular website platforms (WordPress, Drupal, and Joomla). PHP code can be found all over the internet. There’s PHP code all over the place, ready for you to download, modify, and use for your purposes.

    Node wins because it has more modern features.

    Sure, there are thousands of excellent open source PHP files available, but some are 12-year-old WordPress plug-ins hoping for a download. There is a dusty, long-forgotten library for every modern version of Symfony that no one updates.

    They were created by programmers who understand that the majority of the intelligence in modern web apps should be pushed to the client. Not only are Node.js plug-ins newer, but they were created using the most up-to-date architectural approaches. Who wants to tinker with code that hasn’t been updated in years for hours, days, or weeks?

    Why put yourself in a box? While JavaScript has a number of quirks that irritate some users, it is, for the most part, a modern language with a modern syntax and a few useful features such as closures. You can easily reconfigure and extend it, allowing you to use powerful libraries like jQuery. Functions can be passed around like objects.

    Where PHP comes out on top: Simplicity (sort of).

    PHP is a simple language with only a few variables and basic functions for manipulating strings and numbers. PHP has just the right level of complexity for a job that isn’t supposed to be complicated. That’s exactly what it’s designed to do. A modern database is a magical tool, so it’s only natural to trust it with the heavy lifting. It’s a simple layer that does nothing but move data from port 80 to the database and back.

    On the other hand, if you’re a programmer who wants to do more than interact with a database and format the results, PHP now allows you to do so without straining your neck. Using this constrains your code to only run on the HHVM, but that isn’t necessarily a bad thing. Hack, a complete language with modern features like type annotations, generics, and lambda expressions, is now supported by Facebook’s HHVM. It moves at a breakneck speed.

    Where Node excels: the plethora of language options.

    If PHP users are content with Hack, they should consider switching to Node.js, as many major languages can be cross-compiled to run in JavaScript. Jeremy Ashkenas’ list of languages that compile to JavaScript is quite extensive. There are even gifts for BASIC or Pascal enthusiasts. There are well-known languages such as Java, C#, and Lisp, as well as dozens of others such as Scala, OCaml, and Haskell. In addition, JavaScript’s cousins TypeScript and CoffeeScript take a slightly different and improved approach to the same game.

    Where PHP excels: There is no need for a client application.

    Build everything up on the server to avoid overburdening that tiny phone’s browser. PHP is optimized to create pure HTML, which works more often than anything else. Why should you use JavaScript in your browser? What if you’re creating a simple, static website that only does what it needs to do without the frills? While all of the talk about using the same language in the browser and on the server is nice, what if you don’t need to use any language in the browser? There are no headaches or glitches caused by misfiring JavaScript threads that try to create a page on the browser from two dozen web service calls because the browser pops it up. What if the data is delivered in HTML format?

    Node wins because service calls are lighter than HTML-heavy PHP calls.

    Node.js is designed to deliver data and nothing but data via web services. There’s a big payoff if you’re willing to put in the time to create a slick browser-side web app. There isn’t a lot of HTML markup, and the page doesn’t have to be downloaded multiple times. While AJAX-crazy HTML5 web apps can have an excessive number of moving parts, they are cool—and extremely efficient. The only thing that moves along the wires once the JavaScript code is in the browser cache is the new data. The only thing that has changed is the information. It’s a good foundation for efficient delivery if your app is complex and data-rich.

    SQL is the place where PHP triumphs.

    Because some of the most stable and well-developed code interacts with an SQL database, all of that power can be easily integrated into a PHP project. With a few changes to your queries, you can switch your code. If MySQL isn’t quite right, Oracle and Microsoft both have excellent SQL databases. PHP was designed to work alongside MySQL and its many variants, such as MariaDB. The vast SQL universe has no boundaries. It isn’t a perfect, happy family, but it is a large one. Not only that, but the database world is gradually improving as developers discover ways to add more intelligence to the database, reducing the amount of work you have to do.

    JSON is where Node.js shines.

    However, Node.js also understands JSON, the standard for interacting with many of the most recent NoSQL databases. If you need SQL access, Node.js has libraries to help you out. Everywhere, colons and curly brackets work the same way. From browser to web server to database, there is only one syntax. That alone will save you a lot of time and aggravation. That’s not to say you can’t find JSON libraries for your PHP stack, but there’s something fluid about working with JSON in JavaScript.

    Where PHP excels is in coding speed.

    For most developers, writing PHP for web apps feels faster: no compilers, deployment, JAR files, or preprocessors are required—all you need is your favorite editor and a directory containing PHP files. Your mileage may vary, but PHP is a good tool to use when you need to get a project done quickly.

    Where Node.js excels: application performance

    The core is well-made and designed to take care of everything for you. When you’re counting curly brackets and parentheses, writing JavaScript code is a little more difficult, but once you’ve finished, your Node.js code will fly. The callback mechanism is fantastic because it eliminates the need to juggle threads. Isn’t that exactly what everyone desires?

    Competition is where PHP comes out on top.

    This can only mean better results. The HHVM and Zend teams are working hard to provide everyone with fast code. Independent benchmarks are popping up, and everyone is pushing their code bases to their limits. The battle for PHP developers’ hearts and minds is still going on.

    Solidarity is a place where Node.js triumphs.

    Competition is beneficial, but fragmentation is inevitable. What happens if your code only runs on one of the two platforms? It doesn’t matter how good the competition is if you have to spend weeks or months rewriting your code. Is it really necessary to have two different code bases? While Node.js had its own splintering a few years ago, the Node.js universe has since reunited with the launch of io.js, giving it the kind of language solidarity that PHP developers may soon crave.

    Basic apps are where PHP shines.

    A few developers have started web apps in the last few years and been frustrated by their slow performance. When all of the packets arrive, they must be parsed, compiled, and then executed—all for the purpose of delivering a few bytes such as the temperature and the forecast. The JavaScript that controls all of those moving parts can be tens of thousands, if not hundreds of thousands, of bytes long.

    PHP is an obvious choice for any team that wants to keep the intelligence on the server and not on the client. The teams building static site generators (463 as of this writing) and stripped-down webpages in the AMP format are part of the backlash against this rococco insanity.

    Node.js excels in the following areas: richness

    Have a little fun! Enjoy yourself. Why should we deconstruct our code and live as if we were a Depression-era victim in a John Steinbeck novel? Another architect, Robert Venturi, came along and retorted, “Less is a bore.” Large, slick websites with JavaScript code are eye-catching, dramatic, and, above all, entertaining. Sure, wasting that much bandwidth on a few bits of data is a bit obscene, but bandwidth has never been cheaper. Building architect Ludwig Mies van der Rohe once said, “Less is more.” To keep the video cards cool during all of the processing, the desktops have multiple fans. Smartphones have more processing power than an entire room of Cray computers.

    Headless is a scenario in which both parties win.

    The PHP code running on the server is referred to as “headless.” Instead of attempting to compete with them, they are relinquishing control of the client and focusing on the server’s back-end. Some of the most popular PHP applications, such as Drupal, have recently taken a look across the aisle and been blown away by the sophisticated user interfaces created by JavaScript frameworks like React, Angular, or Vue.

    A progressive web app built with the latest JavaScript framework is used on the client side. The old PHP code acts as a front door to the database, double-checking requests, cleaning up data, and providing all business logic. It sends an AJAX request to the PHP code when it requires information. If you’ve got a lot of PHP code running on the server, this could be a good way to get the best of both worlds.

    This may not make sense for someone starting from scratch, but if you’ve been using PHP for a long time and want to progress slowly, this can be a good compromise.

    Microservices and serverless are two areas where both win.

    The lingua franca that unites them all could be the language of POST or GET. Both solutions break the work down into dozens of smaller services or functions, each of which can run on its own and stay in its own lane. The emerging microservice or serverless paradigms provide a way for JavaScript and PHP code to coexist and get along on the same server. The rest of the code, especially the newer parts, will be written in Node.js. Some parts of the app, usually the older and more stable ones, can run PHP.

    • Development of software
    • JavaScript is a programming language that allows you to
    • PHP is a scripting language.
    • Peter Wayner is a contributing editor at InfoWorld and the author of over 16 books on topics ranging from open source software to autonomous cars, privacy-enhanced computation, digital transactions, and steganography.

        2019 IDG Communications, Inc. Copyright ©

Leave a Comment