DEV Community

Node JS vs PHP

Opemipo Disu on May 23, 2020

There are arguments out there on the best technologies but there is just one argument that is really common among web developers which is Node.JS V...
Collapse
 
szilardszabo profile image
SS • Edited

Node.js is not a framework in any sense. It is a run-time environment that has a javascript interpreter at its core.

PHP is on the other hand is not a general purpose language.
It is in practice not well suited anything other than web development.
Can you tell me any other typical usage of the PHP language? No you can't!
It was originally developed as a simple scripting language only for web development built on top of C libraries and functionalities.
In so many respects it still a scripting language.
From this perspective PHP can be more easily called as a "web framework" than Node.js.
Actually Rasmus Lerdorf the inventor of PHP said it a few times that PHP is a web framework.

Actually in this respect Node.js is exactly the same, it main purpose to provide you with a scripting language and its runtime interpreter is built on top of some C libraries and functionalities.
So in this respect there is no difference at all...

Anyway, all of this actually quite the opposite of what your are writing.

Please note I have no preference in PHP or Node.js, I am using both and I am not biased towards any of them, so it is not about saying that one is better than the other. What I am writing are just simple facts.

What you need to be very careful of is that if you look at laravel and an express code that looks very similar, they actually can result in a very different way of execution flow in the runtime. For an inexperienced developer this can result in a very unexpected behaviour, especially when it comes to javascript as it executes very differently from PHP. This is what needs to be emphasised in a comparison, and this is what is not there in your article at all. I suspect the reason of this - and it is because of my experience with so may people - is that you do not know any of the reason of this big difference. You have no idea of most probably...

Also I don't see any evidence that Node.js scales better or faster than PHP, it really depends of the use case.
They both scale very well and fast enough provided that used for the right task which is different for both.
(Actually if you want to compare the differences, you can write about the very different type of tasks the two things are good for, and then you and your readers would start to understand the difference.)

Node.js is by default only fast if it does not do any CPU heavy computation, so in this respect in generic case it has exactly the same performance as PHP. (As PHP also not very good for CPU intensive stuff)
Node.js is only fast and scales well in handling concurrent requests if nothing CPU heavy is involved, which is a special case. So you can't say it is fast in general. Only in this special case.
The big difference here is that if you do very CPU intensive task with PHP it can still survive because of it's designed for multi threading on the other hand Node.js by default with its single threaded event loop just stop serving any request if a CPU heavy task hold up the event loop. Its single thread would become unresponsive.
Notes:
1, Multithreading in PHP means that when running as a webserver module that the webserver has a thread pool of your script ready to serve requests. This means that the same script you write runs in multiple instances.This is not natively supported with Node.js when you simply serve the requests using its http and similar modules or express.
2, CPU intensive task is for example a task that needs a lot of number crunching, like encryption, calculating complex equation, running recursive calculations etc..

If you want to compare the two thing and emphasise the big difference between them - and there are huge differences between them - then you really need a different approach.
I recommend you to gain more fundamental knowledge in both before try to compare.

What you need to talk about when it comes to the huge difference between the two things is their architectural design.
In short Node,js architecture follows single threaded event loop model that is very different from PHP runtime model which natively supports multi threaded synchronous execution flow.
This is one huge difference and the cause that the two thing behave very differently when it comes to scalability and speed in very different scenarios of course.

I advise you and everybody who wants to know the real difference to start digging into these topics to understand the basics of program languages and the design of their run time architecture.
Unfortunately of course it needs competency in these fields which is unluckily very scarce.

This kind of inadequacy and inability to properly compare things, giving false impressions and spreading false information are all the result of the so many so called developers today in this industry lacking the most basic knowledge of the field which is very bad news for competent, educated developers.

Your article in this current form is written very inadequate and incompetent way, giving the usual false impressions to everybody about PHP and Node.js.
What worse is that it is in absolutely in sync with all the popular false beliefs and false information circulating everywhere on the web about these two thing,

First of all I advise people to start using these things as they are without any frameworks on top of it. E.g. Node.js without express and PHP without laravel or similar to understand what is the native behaviour of these languages and gain some basic understanding, because lot of people confuse a framework with a program language and its run time.

Collapse
 
a_regularjeff profile image
Jeff

Well... I think your points are kinda weak to say that PHP is better than NodeJS, first of all it will depend on what are you building but in my experience I have never been in a spot where I'd reacher choose php over Nodejs. (Node is easier to set up since you don't depend on anything like apache to run a server... )

Collapse
 
yhoung24909577 profile image
obiabo Immanuel

Great article
most people hype php and some hype node ...
arguing about the best technology sucks but using them to get what is best for you is the aim

Collapse
 
ouailsalem profile image
Ouail

You can't say that php is better than node js.
It depends on the project

Collapse
 
codeamancoder profile image
codeamancoder • Edited

I'm not quite sure what the reason is to compare such different aspected languages, simply it depends what you are building for, shortly use both to get more benefits.
Well, we have a huge project that has been being developed since 6 years, there are many backend and frontend tasks which was built with PHP aimed for heavy tasks, and microservices for our API solution to communicate with 3rd party apps and products with node express.js as well as realtime operations with node socket.io.

Collapse
 
dividedbynil profile image
Kane Ong • Edited

Nowadays there's a lot of hype about SSR in web development, I really like to remind them the king of SSR is PHP (SSR with a CSR framework is very unnatural in my opinion). Anyway, as long as the tool can get things done, it is a great tool.

Collapse
 
andrejika profile image
Vasilevs P.

Apologize, but too few arguments to have the proper discussion. I'm absolutely love Laravel and Express.js, they're built on top of Rails, so what's the deal? You forgot to compare Python/Django also.

Collapse
 
gnio profile image
Gnio

Loved to read this article. I'm a beginner so I couldn't understand most of it. Like, most most of it. It will make sense some day. :D

Thanks for sharing!

Collapse
 
coderoflagos profile image
Opemipo Disu

Thank you

Collapse
 
ranggarifqi profile image
ranggarifqi

Most developer loves PHP than Node js

Do you have the data to support this conclusion ?

Collapse
 
saint4eva profile image
saint4eva

PHP is faster than Nodejs

Collapse
 
jitheshkt profile image
Jithesh. KT

CMS stands for content management system, not custom management system.

Collapse
 
mustaphee profile image
Yusuff Mustapha

Lol, true. Not even Mark will do thatπŸ˜‚πŸ€£

Collapse
 
kushal-niroula profile image
Kushal Niroula

Just curious, how did you use associative arrays in js? As far as I know, js does not have an associative array. The closest thing is to use objects to simulate it. Did you use objects for that?

Collapse
 
michi profile image
Michael Z • Edited

You can use Map or objects depending on your use case.

Collapse
 
assassin9 profile image
Asjark

All hail php!!!