PHP vs Node js ... Let's be honest: Node.js couldn't kill PHP. Why?

Node.js is a tool for back-ends creation. As well as PHP.  But questions like “Which is better for back-end - PHP or Node js?” or even “Is Node js killing PHP?” or "Is NodeJS eating PHP Market?" are asked frequently.  Let's be honest: Node.js couldn't kill PHP. Why?

May 05, 2017
PHP 7 vs Node.js
According to the “Developer Survey Results 2016”, which was conducted by STACKOVERFLOW, among over fifty thousand developers from all over the world, PHP is more widely used back-end technology compared to Node.js among back-end developers. Source: stackoverflow.com/research/developer-survey-2016

 

 

PHP 7 VS NODE JS: TERMINOLOGY DIFFERENCES

  • PHP is a programming language for writing code and it is also at the same time an interpreter of PHP code to the operating system (like Linux or Windows) installed on the server. Node.js is not a programming language; it is rather an interpreter of Javascript code (due to the Google Chrome V8 engine) to the operating system installed on the server. Node.js is not just an interpreter, because it is extended with libuv library that focuses on asynchronous I/O.
  • To execute PHP code on the server, one needs to install PHP package  or Facebook HHVM package. To execute JavaScript code on the server, one needs a Node.js installer.
  • As a part of the real-world web applications, PHP code should work in conjunction with web server. PHP has a built-in web server but it is not recommended for use on a production hardware server. As a rule, developers use Apache web server, Nginx web server or any other servers that support FastCGI or CGI protocols. As for Node.js, it has built-in tools for creating servers, hence it can work without the help of external servers. However, as a rule, Nginx server is used as a reverse proxy before Node.js server on a production hardware server.

PHP was created in 1994, Apache – in 1995, Javascript – in 1995, Nginx – in 2002, and Node.js – in 2009. Why was it necessary to create Node.js? Let’s take a look at the history of creating Node.js.

PHP 7 VS NODE JS: BEHIND NODE.JS CREATION

As everybody can see from the first presentation of Node.js in 2009, Ryan Dahl, the creator of Node.js, criticized:

  • The limited possibilities of the most popular web server in 2009, Apache HTTP Server to handle a lot of concurrent connections (up to 10,000 or more) and
  • The most common way of creating code (sequential programming), when code either blocked the entire process or implied multiple execution stacks (read as “more hardware and software”, more money) in the case of simultaneous connections.

 

PHP 7 vs Node.jsPHP 7 vs Node.js
PHP 7 vs Node.jsPHP 7 vs Node.js

 

As Ryan Dahl noted, Node.js and Javascript were able to solve this problem at that time at no extra cost:

  • Node.js provides a purely evented, non-blocking infrastructure to script highly concurrent programs.
  • Javascript is designed specifically to be used with an event loop: anonymous functions, closures; only one callback at a time; I/O through DOM event callbacks. The culture of JavaScript is already geared towards evented programming.

Ryan Dahl‘s presentation was regaled with ovations. One of the most common web applications' performance problems was resolved! No additional hardware! No additional monthly hosting costs! The only strict requirement was retraining programmers to teach them using event loops, callbacks, and non-blocking I/O.

PHP 7 vs Node.js
PHP 7 vs Node.js

 

Some large companies expressed interest in the implementation of Node.js into their live project. As was reported, the transition from Rails to Node.js improved the scalability of LinkedIn’s mobile backend infrastructure: the company was able to move from 30 servers down to just three, while still having enough headroom to handle roughly ten times their current level of resource utilization.  There’s been a lot of talk about PayPal moving from Java to Node.js for an application platform. The Node.js app was built almost twice as fast with fewer people, written in 33% fewer lines of code, constructed with 40% fewer files.

PHP 7 VS NODE JS PERFORMANCE

In 2015, PHP 7 with new long-expected features was introduced. Possible differences in performance is already not an issue when one compares “Node.js” technology with “PHP” technology.

 

PHP 7 vs Node.js

PHP 7 VS NODE JS: PHP IS ALIVE AND HAS LEARNT ITS LESSONS FROM NODE.JS TO BE STRONGER THAN BEFORE

A lot has changed since 2009. The WebSocket protocol was standardized in 2011 and NGINX server started to support it. PHP programmers have learned to use event loops, callbacks, and non-blocking I/O with ReactPHP.

So, today’s experienced developers can choose what tool to use for any particular project. What do they take into account when considering what tool to use?

Andrey M., a software developer at Belitsoft Company (10 years of experience with PHP, 1 year – with Node.js): For some tasks, Node.js may be faster than the “standard” webserver with PHP because it runs as a single thread with non-blocking IO for each connection, hence there is no memory overrun. Due to this, Node.js is useful in cases when there is a need to process data in real-time (chats, games, video, big data streams without logic), however Node.js can also be used for creating CMSs, ecommerce and other web applications, which are usually developed using PHP. Node.js is also able to perform heavy loaded tasks, but these tasks should be moved away from the Node.js’ main thread using appropriate tools such as callbacks, workers etc.
On the other hand, tasks for which Node.js is an excellent choice, can be performed using PHP. For example, ReactPHP enables developers to write PHP-based socket server to process requests constantly as well as Node.js does it (yes, Node.js is faster, but the fact is that PHP can also do it). The same thing with Workers (the classes responsible for running asynchronous jobs and synchronizing their results) or Amp (libraries that are used for writing non-blocking asynchronous code). Hence, it is easy to create long running processes using PHP. Additionally, there are a lot of tools for supporting and managing these processes (such as supervisord).
Finally, Node.js can be used as complement to the PHP (or to another programming language) with the aim of reducing load on a server and performing program (or, as one can say, PHP can be used as complement to Node.js :))) For example, the first version of a website was built in PHP and then the client asked to develop a real-time system that gets data from stock data systems. I would prefer to develop that real-time system as a separate application using Node.js (and then integrate it in the PHP-based application).
So, the same tasks may be performed either with PHP or with Node.js. The question "what tool to use" is a question of personal preferences. I use Node.js for tasks involving big data flows and PHP for tasks involving a complex logic, high-load tasks, for dealing with external utilities, applications and OS. PHP has a lot of different libraries, frameworks, and it is more standardized than Node.js (however, Node.js is not far behind and continues to gain momentum). In general, I think it is a good idea not to oppose them, but use them cooperatively.

Sergey K., a software developer at Belitsoft Company, participant and award winner of various hackathons (8 years of experience with PHP, 1 year – with Node.js): Comparatively low entry barrier for writing PHP code means there are many PHP specialists on the market, hence, it is pretty easy to find developers for PHP-based projects. Due to age factor, PHP has more production-ready modules, frameworks and CMSs. It means if a client needs a product and there are ready solutions in PHP, then it is better to use PHP because it will be cost effective.
I think the entry barrier for writing Node.js code is higher. Node.js uses JavaScript, but it does not mean that any front-end developer can easily start writing back-end using JavaScript. And it does not mean that any front-end developer can use the same code snippets both on the front-end and back-end.
PHP and Node.js are tools that can very often be used interchangeably for the same tasks. However, Node.js is often used for real-time applications, web sockets and workers (especially, persistent workers), and rarely for high-load tasks and computation. From the scalability perspective, there are no big differences between PHP and Node.js, it is more important to consider the project’s architecture.

It is always interesting to hear what other experienced developers think. Here is what we have gathered from the web.

PHP 7 vs Node.js

PHP 7 VS NODE JS: SOME OPINIONS FROM THE WEB

Jonathan: We are planning to run a large-scale E-commerce marketplace (eventually a pool of websites, one for each country) and we are trying to decide on whether to use PHP + Apache Stack or go with Node.js. We plan to get 5000 peak simultaneous connections for our "product search" function. What would you suggest?

Thomas Hunter (a Senior Node.js Engineer): I'd lean towards PHP as you're probably not going to benefit from any real-time communication, shared data between different users, etc. Also, if Node crashes, it could cause a lot of trouble, especially since most of what you are doing is transactional, but if one PHP execution fails, it's much less dangerous. There's also a lot of PHP eCommerce applications and libraries out there already that you could make use of.

Azat Mardan: There are many better alternatives to PHP, e.g., Ruby on Rails and Django, however nothing is as approachable as Node.js. I worked with many technologies including Ruby on Rails, Python, Java/J2EE, VB, ASP, Perl and of course PHP. However, my focus during the past couple of years has been dedicated solely to Node.js and front-end JavaScript frameworks like Backbone.js. So my opinion might be biased.

Juan (a developer with experience on Node.js, PHP and Ruby): I developed an amazing and faster webRTC solution using Node.js and an astounding video portal using PHP that receives 1, 5 million visits every day; however I don’t think that I can achieve these projects’ goals by interchanging those technologies.

Laurens: I still doubt whether to use Node.js or native PHP sockets. Wouldn’t it make more sense to use default PHP instead of Node.js? As far as I know Node.js is limited and barely has support for a good MYSQL connection, isn’t PHP more optimized for this? I know that Apache is slower than Node.js, but for what kind of application is it better to use Node.js? I do not have any idea of Node.js advantages over PHP Socket, perhaps the speed, but I will be using Nginx.

Gonzalo Ayuso (an experienced web architect and web developer): Websockets with Rachet (PHP) works fine. I started to play with socket.io (node.js) some time ago and now I’m very comfortable with it. I haven’t done any in-depth benchmark to choose one or another, indeed. Socket.io for example, has a very fail-over system to work with old browsers (it works even with IE6), but Rachet works fine with newer ones. As you said, to perform traditional SQL operations is a bit of a nightmare using node. This is mainly because of the functional nature of js (OK js is not a fully functional programming, but it’s very close). I don’t have this problem because I always rely on backend server (PHP for me) and I only use node.js for the strictly real-time operations. My node.js servers for websockets are just a proxy and all the logic are bypass to the backend. Anyway, if you choose PHP or node.js for websockets you must take into account that you will need to handle two different servers. That’s means share authorization layer and things like that.

Since WordPress.com has moved to Node.JS, do you think Laravel will also look towards that option even though PHP 7 is here?

Dayle Rees (a Laravel Framework contributor and developer): For a long time PHP was the butt of many language jokes, but I honestly feel that it’s becoming not only a popular language, but a powerful one. PHP7 is great. The speed boost is one thing, but having optional support for full type hinting is a game changer. We’ve also got modern tools like Laravel and Composer, breathing new life into the language and its supporting community. With this in mind, I think it’s unlikely that Laravel will move from PHP. I think it’s more likely to gain further integration with front-end tools to provide a complete application building platform. That’s where I see it heading in terms of future expansion. I’m sure Node will continue to excel when dealing with microservices and threaded applications.

Belitsoft is a custom software development company (offshore software development). We provide custom application development services (PHP DevelopmentNodejs Development etc.)

Subscribe to Belitsoft's Blog for Entrepreneurs

Join successful software startup founders! Get insights from growing companies like, where to get an idea, how to validate it, how to launch, and how to hire people - everything. Enter your email address below (no spam):

Email *