DEV Community

loading...

Node.js vs PHP

sandipmavani profile image Sandip Mavani ・5 min read

There’s no doubt PHP is the most known and commonly used language for server-side scripting. Before Django and Ruby on Rails gained popularity (2005-2006), there was hardly any more suitable option for back-end than PHP. However, the tech world is fastly evolving in the direction of simplicity (“Javascript everywhere”) what used to be the language of the front-end has successfully expanded to the back-end. So now we are facing the popular back-end dilemma “Node.js vs PHP”. Let’s try to solve it together!

PHP
PHP (Hypertext Preprocessor) was created as a scripting language for back-end web development in 1994. For almost 10 years, it had been the only option for a back-end developer. Although lately, new technologies emerged, PHP wasn’t at a stop as well (the last stable version was released in January 2018). As of the latest statistics of 2018, more than 80% of websites are built with PHP (though some websites are built with more than one back-end language).

Node.js
Node.js is an open source run-time environment for back-end scripting in Javascript powered by Google’s V8 JS engine. It was created in 2009 and came up with the main advantage — Node.js allows to perform asynchronous programming. It can handle thousands of requests operating on one thread, which means no waiting until the previous command is completed. Although the percentage of websites that are built with Node.js is comparatively low (0,4%), it’s fastly becoming popular among developers.

Node.js vs PHP: Differences

Languages
Starting our Node.js vs PHP discussion, we can’t avoid talking about the specifics of two programming languages Javascript and PHP. In terms of syntax simplicity, PHP wins. Such an essential operation as an interaction with a database requires less and simpler (quite alike to HTML) code in PHP than in Node.js.

However, the simplicity of PHP doesn’t come without certain restrictions. On the other hand, some complexity of Node.js is balanced by more possibilities of using various Javascript libraries for two-way asynchronous scripting of client and server.

Also, Javascript syntax of Node.js is obviously a reason to choose it for those who come from the front-end side. So a full-stack developer may take an advantage from the code reusability options as a result of scripting both client and server sides in Javascript.

Requirements
Although PHP has a built-in web server (which is available since PHP 5), it’s not a full-featured one and can be used only for testing purposes. The most commonly used with PHP servers are Apache and NGINX. One need to get it installed and configure (by installing corresponding PHP components) a server before actually start working with PHP.

Node.js doesn’t require any external servers. You can get started by installing Node.js. After that, you just need to install http-server packages via npm (which is just a few words of code) to use built-in web server tools. Also, installation of Express.js a Node web framework may help to do more than just trivial handling HTTP requests.

When it comes to libraries, both PHP and Node.js have built tools that allow to install and manage them from the command line (Composer in PHP and npm in Node.js).

Scalability
Comparing Node.js vs PHP scalability, it turns out that both both technologies are scalable. It’s totally possible to build large scalable applications with either PHP or Node.js. Still there’s a difference that lies in the efficiency of building scalable application architecture.

PHP is supported across most popular content management systems (such as Drupal, Joomla, WordPress), which makes it an often choice as a tool for building blogs and e-commerce web applications. In contrast, Node.js efficiently serves as a tool for creating scalable dynamic solutions that deal with numerous I/O operations. It’s also possible to scale Node on multi-cores systems, though with more efforts.

Handling data
When it comes to working with data, the main arm of Node.js is JSON, which is, on one hand, understandable by NoSQL and, on the other hand, is used for passing data on client side. And if early version of PHP had some troubles with JSON, it’s not an issue any more as JSON support is included in the core since PHP 5.2.

However, the main advantage of PHP when it comes to working with data is that it’s compatible with all the hosting services. And the latter is very far from the reality for Node.js, which makes a good fit with only a narrow range of hosting services.

Node.js vs PHP: Similarities

Languages
Both PHP and Javascript are interpreted languages that can be used not only for web programming but general purpose scripting. They require a certain run-time environment to be used for scripting (in the case of Javascript it may be either browser or server, which depends on whether it’s used for client- or server- side scripting).

Performance
Although Node.js is always highlighted as a high-performative one because of its asynchronous model, PHP has also evolved in this direction. With such libraries as ReactPHP, it has become possible for PHP to be used in event-driven programming as well.

On the other hand, most servers function on multi-threading, which brings difficulties to work with Node.js. In such cases, asynchronous programming becomes more of an obstacle as not every intermediate-level programmer has enough expertise in it.

Even though Node.js is faster (some Node.js vs PHP benchmarks can be found here), PHP 7 and Node.js are almost at the same level in terms of pure performance. In other words, handling asynchronous I/O operations isn’t something that can make Node.js a winner in Node.js vs PHP performance competition.

Conclusion
Both PHP and Node.js obviously have their own advantages and disadvantages. However, it doesn’t mean that you can’t build the same applications using either PHP or Node.js. So how can you pick one of them? I believe that the best way to make a choice of Node.js vs PHP 2018 is to pick the one that is most compatible with other technologies you use for developing web applications. It will save your time and resources.

Apparently, if you are about to use most of MEAN (MongoDB, Express.js, AngularJS, Node.js) stack tools, numerous Javascript libraries (Angular, React, Backbone, etc.) or develop SPAs (Single Page Applications), Node.js may be a better fit for your project. On the other hand, if you take advantage of the most of LAMP (Linux, Apache, MySQL, PHP) stack technologies or other servers like SQL, Oracle, Postgresql you may need to consider PHP in the first place.

Although discussions around Node.js vs PHP don’t seem to cease any soon, the important thing to remember is that there’s nothing unique that you can do only with one of them they are interchangeable. However, you can always orient at the level of development expertise and stack of technologies that are to be used in the process of development.

Discussion (22)

pic
Editor guide
Collapse
dbelyaeff profile image
Dmitriy Belyaev

These are two separate worlds. I think it's a choice of habit.

I'm programming in PHP from 2003.

Then Rails. Then Node.JS.

All of them are differ. They can be used for the same purposes, but any of them has its own advantages and disadvantages.

No one will deny you to use any stack of technologies in your work.

And if you're not doing large scalable projects performance difference between those languages is not so noticeable.

Collapse
buphmin profile image
buphmin

So here are a few thoughts of mine using php/node for both web and non web projects.

For this I am assuming php 7+ and node 8+.

So for web I would give node.js the edge, and for an api only node.js for sure. In my experience node simply has better performance in both req/sec and, imo, more importantly response time. A caveat here is if you are doing heavy cpu processing (especially if you can use a php c extension), because php is typically faster in that regard.

So in my testing (not the most scientific method mind you) on my local linux vm using I was able to handle 40x more concurrent requests on node before degrading response time to over 200ms. I have played around with php-pm with a Symfony project and I was impressed with performance, but it would frequently error for no reason I could figure out.

Now I will say the frameworks for php are better (if you like using a framework). I think this has to do with how long php has been around and that people tend to contribute to fewer but very well maintained frameworks in php. Node has a metric crap ton of frameworks and they all seem to have some issues. PHP will also be easier to maintain, though typescript is making node easier.

Overall though if you are looking to make a website that is not internal use only then the performance difference is really hard to ignore. Especially since js is not all that much harder.

Collapse
perttisoomann profile image
Pert Soomann

Not played around with Node that much, but I got the feeling, correct me if I'm wrong, PHP runs every request independently, so breaking code on one URL won't affect other visitors, while on Node project you could crash the whole service, and would need to restart it for the whole site/app to be available again?

Collapse
kejtenoviot_sin profile image
Кејтеновиот син ♦

You can make micro services with nodejs so that if one crashes, they don't all crash.

Collapse
phunckys profile image
теле • Edited

Both replies to the original comment are workarounds. PHP provides this out of the box, while with Node.js you need to create an architecture around handling such cases.

Collapse
vardiak profile image
Nathan

Basic error handling can prevent this from happening. It's native if you are using Express.

  • microservices make that even less possible.
Collapse
bgadrian profile image
Adrian B.G.

I had to do a comparison for a new startup actually and (unfortunatelly) I had to choose between these 2 languages, and putting aside the local market available developers, PHP really didn't brought any advantage on the table.

It may behave better with *SQL databases (I found this reason as stated by other devs, but I haven't seen any data on it), but rather then this NodeJS is better at ecosystem, latency, re usage of the code between frontend/backend, and a few others. I will have to search my paper on it.

But between NodeJS and compiled back end languages like Go,Rust and Java enterprise Apps I really don't see PHPs place in any med/large app/team in 2018 and beyond sorry. I think it is still popular because of the sheer amount of developers that know PHP.

Packages

I have not played with composer in 5yrs, but I bet is better than the NPM as a tool and as an ecosystem (which is a total mess).

However, the main advantage of PHP when it comes to working with data is that it’s compatible with all the hosting services.

No real project/customer that has more then 3$ monthly will use a shared hosting, so it is an invalid argument. For more serious hostings managed env nodeJS has more supported environments actually (including Cloud Functions).

handling asynchronous I/O operations isn’t something that can make Node.js a winner in Node.js vs PHP performance competition.

Sure thing it is, is actually why Node will win in intensive web apps. Should I remember you that I/O include any DB and API query?

They require a certain run-time environment to be used for scripting

Even if PHP 7 is a lot better then the previous, let's just say that I trust V8 performance team more than I trust the PHP devs on the long run.

across most popular content management systems

I cannot believe you mentioned CMS and Wordpress in the "Scalability" subtitle.
PHP-FPM is easier to scale then nodeJS that is for sure and well supported. For nodeJS a simple similar alternative would be single-core machines with auto-scaling LB.

Collapse
thinsoldier profile image
thinsoldier

No real project/customer that has more then 3$ monthly will use a shared hosting, so it is an invalid argument.

There are hundreds of thousands of freelancers living acceptably comfortable lives by having dozens of clients who are willing to pay a few hundred dollars as-needed but unwilling to pay any kind of monthly fee. And even if they did, not everyone has the time to be an expert in all the areas of expertise they already dabble in and then have to learn to do their own devops on top of that. They'd rather pay monthly for real professionals to manage their server for them IF their clients really needed that, but most of their clients do not need it.

Collapse
thinsoldier profile image
thinsoldier

Such an essential operation as an interaction with a database requires less and simpler (quite alike to HTML) code in PHP than in Node.js.

I'm a php fan but I have to disagree here. I've watched tutorials on using MySQL with Node and it seems just as easy as php.

Collapse
yatikasharma7 profile image
Yatika Sharma

Clear Comparison between the two. However sometimes in some situations developers have to make a clear choice between the two i.e. Node.js and PHP to build any new website, if your doubts are still not clear then kindly go through this article hackernoon.com/nodejs-vs-php-which...

Collapse
lluismf profile image
Lluís Josep Martínez

"For almost 10 years, it had been the only option for a back-end developer." You seem to forget ASP, JSP ... popular template engines for C# and Java.

Collapse
nezteb profile image
Noah Betzen

I personally prefer PHP 7 to Node. Here is a cool video by the creator of PHP about the language: youtube.com/watch?v=rKXFgWP-2xQ

Collapse
nathandaly profile image
Nathan Daly

Just to add to this discussion, PHP 8 will be a true JIT compiled language.

Collapse
shalvah profile image
Shalvah

Small correction:

After that, you just need to install http-server packages via npm

You don't. They're included with Node.

Collapse
miodragt profile image
Miodragt

plz look at link where I am use react and php
academia.edu/38021343/Pozivanje_ph...

Collapse
devmazee2057282 profile image
dewbiez

I've used both PHP and Node.js, and I recommend both of them.

Collapse
biros profile image
Boris Jamot ✊ /

I didn't understand the conclusion: who wins?

Collapse
drewknab profile image
Drew Knab

No one wins. There’s just your needs and the needs of your project and choosing a server-side language appropriately.

Collapse
xowap profile image
Rémy 🤖

I think there is a mistake, PHP means "Personal Home Page". It's a weirdly common mistake though, even the PHP docs seems to make it.

Collapse
christophwurst profile image
Christoph Wurst

It was just called "personal home page" in very early versions. Ref en.wikipedia.org/wiki/PHP#PHP_3_and_4

Collapse
hesamzakerirad profile image
Hesam Rad

Helpful article, thank you for putting the time to write it.