Both languages have evolved from their original releases, impressively maintaining a high level of relevance in the ever-changing technological landscape.
Microservices, containerisation and code parallelisation are just some trends that did not exist when the languages were introduced.
Likewise, devices have evolved with the advent of ultra-portable tablets and smartphones.
We are now in 2021 and a very relevant question one should ask is: which programming language should be used today? Do there exist particular use cases that suit one over the other?
This article will not only address these questions, but also break down various differences of the languages – the ecosystems, prerequisites to learning and performance metrics.
If you have endeavoured into web site development you would have undoubtedly worked with one or the other. PHP exclusively runs on the server (or on the back end of a web app), and therefore does not run in the browser (or the front end).
PHP processes web page requests on the server and generates the HTML code to send back to the client side, commonly being the web browser.
This process paved the way for features like template engines (the most popular being Twig) to efficiently embed dynamic content into a HTML page) and core MySQL integration for database management, to store and manage data needed by these dynamic HTML web pages.
This worked well for personal blogs, portfolios, storefronts and other small websites.
PHP is an open source standard with the programming language itself along with a collection of extensions being documented on PHP.net; a comprehensive wiki and release channel for PHP web developers to refer to.
The open source nature of PHP has garnered a very large and passionate internet community of PHP web developers that have contributed to its success and relevancy.
Major browsers these days adopt Apple’s WebKit engine alongside Google’s V8, where features have more parity than ever.
Node has experienced considerable success; it offers various advantages over PHP that will be discussed throughout this article.
However, PHP is still a solid solution; PHP is reliable, well-known and does the job well for some use cases. Before delving into the differences of these web development mammoths, let’s review how the market currently feels about each language to gauge their popularity and relevance in more detail.
What we can see with PHP from W3techs is that PHP is very stubborn at maintaining its market share.
At the time of writing (May, 2021) PHP still has a dominant 79.2% share as the server side programming language for web sites that W3techs sourced. We can further observe via the historical trends chart that PHP is, and will be, the dominant web server scripting language for the foreseeable future.
But why is this? PHP as a server side scripting language powers popular frameworks like WordPress, that Search Engine Journal recently reported accounts for 39.5% of all websites. Corporations like Wikipedia, MailChimp, Etsy and Slack all use PHP for their web pages, that are served via a LAMP stack or similar setup.
In comparison to PHP, Node.js currently has a 1.2% share as the server side language of choice. But unlike PHP, that share is rising with some momentum, gaining around 0.4% from a year ago.
The next section will explore how the technologies differ in their performance and how Node.js in-particular has an edge in asynchronous processing.
What is interesting though is that Node.js does consume considerably more resources than PHP on the server, notably CPU and memory, so the additional speed does come at a cost.
PHP code is synchronous by default, which means the code is sequential; individual tasks must be completed before another is started so the CPU can only handle one I/O task at a time. Tasks are queued whereby the next task is executed once the previous task completes.
One may assume that asynchronous code for PHP may boost performance, and one would be correct. There are PHP extensions that enable asynchronous API calls such as Swoole, an elegant API that supports HTTP server, WebSocket server, and TCP server configurations, as well as others.
Although solutions like Swoole do exist, more configuration is needed to set up the extension that increases the learning curve of using PHP.
Not only this, the developer is tied into the API of the library and will need to familiarise themselves with each API call and the library’s overall capabilities. This adds considerable more time to the onboarding process and takes away some of the user-friendliness that PHP has consistently offered throughout its history.
Asynchronous PHP performance benefits
This will maximise hardware utilisation: Instead of having to run multiple synchronous PHP processes (another way to bypass the synchronicity of PHP), one asynchronous process will utilise the same runtime and therefore use system resources more efficiently.
Asynchronous PHP could also reduce bandwidth and resource allocation (mentioned in the previous point), thus decreasing your monthly server costs.
Before async and await, promises were handled with callback functions that were attached to the promise handler – see then() for an example of this.
With PHP, a single file can be executed just by having PHP installed on the system. Many hosting services come preinstalled with PHP these days, in addition to an easy installation process on desktop operating systems too.
The learning curve however does increase for developing production applications, with Laravel still being the dominant framework of choice for PHP developers.
Node.js requires learning additional boilerplate
Additional server-side setup is required with Node.js; servers need to run as processes managed by process managers (PM2 is a popular one).
There is no clear winner here, although NPMJS does have a stronger web presence with better metrics, search capabilities and overall UI, making it a more streamlined product for browsing code.
PHP still dominates the personal blogging space due to WordPress popularity, but also because there has just not been a good enough reason for small-scale websites to migrate their underlying source code.
For hobbyists, WordPress is a well-known name that gives users theming, a comprehensive and expandable Content Management System (CMS), multi-platform support with storefronts, portfolios or other small-scale requirements.
Laravel, Symfony, Zend and CodeIgnitor are widely used PHP frameworks, along with WordPress. All these libraries offer front end solutions that are tightly integrated with their backend Content Management Systems.
This unifies the stack to writing code for just one underlying specification, and makes development easier for the web developer. This unification is also extended to mobile applications with libraries like React Native.
PHP does a great job for serving dynamic web content to the client side website development,, that traditional websites like personal blogs, storefronts and portfolio pages are well suited for.
There are a wealth of PHP based libraries to create and customise these kinds of websites, and this will suffice for many people without worrying about bottlenecks in performance, speed, and other bottleneck categories.