Exploring the differences and applicable use cases of PHP vs JavaScript in a 2021 comparison
PHP (Hypertext Preprocessor) and JavaScript are two major web development technologies that have been around for over 2 decades, with the former being conceived in 1994 and the latter in 1995.
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.
Let’s firstly get a high level overview of how PHP and JavaScript are used.
Read up on all the in-demand programming languages of 2021 from our recently published article.
A Brief History of PHP and JavaScript
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.
JavaScript added interactivity in the browser
JavaScript runs on both the server and in the browser; this has been the case since 2009 when the Node.js JavaScript runtime was launched by Google, originally created for their Chrome project.
Based on the ECMAScript specification, JavaScript was originally designed as a general purpose client side scripting language for the web browser introduced by Ecma International, a non-profit standards organisation that maintains JavaScript’s underlying specifications.
Even though this standard is maintained by Ecma International, the various JavaScript runtime engines that are used today are open-source projects. Chrome’s V8 Engine, Mozilla’s SpiderMonkey and other JavaScript engines exist as open source projects.
Major browsers these days adopt Apple’s WebKit engine alongside Google’s V8, where features have more parity than ever.
For the first decade or so, both languages complemented each other very well. JavaScript (commonly with the JQuery library) would add interactivity to the web page served via PHP code. With this pipeline, PHP and JavaScript work hand-in-hand for truly dynamic HTML.
JavaScript libraries have now superseded JQuery to some extent, offering a more advanced development and deployment pipeline with highly optimised production bundles.
JavaScript for the server [Node.js] changed everything
Node.js changed everything for the relationship between PHP and JavaScript; Node could directly compete with PHP as server side programming languages, expanding its role in web development from solely a client side scripting language.
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.
PHP vs JavaScript: Relevancy and Market Share
Comparing front end JavaScript with PHP would not make much sense in a head-to-head comparison as they solve fundamentally different problems. What we can compare though is JavaScript (via Node.js) usage vs PHP usage statistics, with both acting as a server side language.
PHP vs JavaScript Market Share
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.
Facebook famously created early versions in PHP before migrating to JavaScript, conceiving React JS in the process from these efforts. Given their current focus on React Relay it is doubtful that Facebook still relies on PHP among their backend scripting languages.
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.
PHP vs JavaScript Salaries
Both JavaScript and PHP are on the lower end of the salary spectrum, with the former’s average global salary of $59k comfortably beating that of PHP at $39k. This is likely due to market saturation given the popularity of PHP, a result of its open source nature and that it is relatively easy to learn.
What is attributing to the consistent increase in JavaScript market share on the server? One reason is performance, and more precisely, speed.
The next section will explore how the technologies differ in their performance and how Node.js in-particular has an edge in asynchronous processing.
PHP vs JavaScript: Performance
The Benchmarks Game offers a range of benchmarks that compare the speed of PHP and JavaScript, with PHP only winning 2 out of 10 of the fastest programs.
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.
JavaScript is asynchronous whereas PHP is synchronous [by default]
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
Adopting an asynchronous PHP library will however improve performance and user experience, processing requests faster and unlock capabilities like Websockets, allowing PHP to compete with JavaScript in real-time server to client web applications.
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.
JavaScript and its async / await syntax
JavaScript is an asynchronous programming language from the off, meaning that other processes can be executed before others have completed.
To make this convention very easy to use in practice, JavaScript supports the async and await keywords that were introduced in the ECMA 2017 specification. Mozilla have some useful documentation that clearly explains these keywords to interested readers.
Async and await allow developers to define asynchronous behaviour on a granular level for their JavaScript functions and API calls, all in an easy-to-read manner. Async and await ultimately make using JavaScript promises a breeze.
Before async and await, promises were handled with callback functions that were attached to the promise handler – see then() for an example of this.
Even though PHP does offer similar behaviour, JavaScript wins in its straight-forward asynchronous implementation.
PHP vs JavaScript: Prerequisites and Learning Curve
JavaScript has a steeper learning curve than PHP. However, to get started with both technologies one can simply run a single script file.
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.
JavaScript is more complex to learn
JavaScript can be run in a browser’s JavaScript console (that Google Chrome has particularly good support for). In addition, JavaScript files can be coded and opened both in the browser or via a JavaScript runtime.
So getting started with both languages in terms of experimentation is straight forward, but JavaScript quickly becomes more challenging to learn, for multiple reasons:
JavaScript libraries and frameworks add complexity
JavaScript libraries like React JS, Angular JS, Vue JS, Next JS, etc, introduce new concepts like props, state, JSX, context, and more to the table, all of which expand upon vanilla JavaScript syntax. This can take weeks or months for the developer to learn on top of basic JavaScript code.
Compilation is now common for client side JavaScript
To expand on point 1, it is common to compile JavaScript code into optimised bundles. This additional compilation process (such as with Webpack) adds yet another layer of complexity to building with JavaScript.
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).
Particular libraries then need to be learned for your server in question, such as Express or Apollo.
The additional work of learning all these JavaScript paradigms are well worth the effort though, with the majority of modern web apps now opting for JavaScript libraries that are becoming more reliant on a JavaScript backend.
Ecosystems
What contributes to the learning curve to any programming language is its surrounding ecosystem of packages. Let’s briefly see PHP vs JavaScript in relation to dependency management.
JavaScript has a huge public repository of code packages named NPM, whereas PHP has Composer and PEAR. Both repositories are extremely strong with millions of packages.
PHP and JavaScript arguably have the strongest communities in programming, and these dependency managers reflect this with the sheer amount of code hosted on them.
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 vs JavaScript: Use Cases
PHP Applications
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.
JavaScript Applications
JavaScript is being adopted for large-scale websites (thousands to millions of web pages) that require optimal performance with cutting edge server side architecture, and out-of-the-box asynchronous support baked directly in the source code.
JavaScript frameworks offer an optimised app bundle for the front end – or client side – that can also be coupled with a Node JS server side architecture too.
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.
Client side applications for blockchain development opt for JavaScript solutions. RxJS is a popular project for composing asynchronous behaviour for these real-time requirements.
PHP vs JavaScript: In Summary
This concludes our PHP vs JavaScript discussion. Both Javascript and PHP, as object oriented scripting languages and server side technologies, are widely used for web development, and it is unlikely to see either of them recede in popularity in the short term.
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.
JavaScript currently has more momentum in terms of adoption, being used for more modern website architectures that need to scale, consume resources optimally, and rely on asynchronous behaviour by default.
JavaScript offers better salaries than PHP for these reasons, but both PHP and JavaScript are sought-after and relevant server technologies. Which language you choose to learn will depend on your goals, although learning both PHP and JavaScript is hugely beneficial for career prospects.