DEV Community

Opemipo Disu
Opemipo Disu

Posted on • Edited on

Node JS vs PHP

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 VS PHP. It's very funny when a framework is competing with a whole programming language. Web developers mainly should look into this before arguing as PHP has frameworks under it too.

Definition of Node.JS

Node.js is an open-source, cross-platform, JavaScript runtime environment that executes JavaScript code outside of a web browser. Node.js lets developers use JavaScript to write command line tools and for server-side scripting—running scripts server-side to produce dynamic web page content before the page is sent to the user's web browser. Consequently, Node.js represents a "JavaScript everywhere" paradigm, unifying web-application development around a single programming language, rather than different languages for server- and client-side scripts. JavaScript is used primarily on the client-side. That’s why Node.JS has become one of the foundational elements of the “JavaScript everywhere” paradigm. Node.jS was created by Ryan Dahl in 2009. Most back-end developers have been using this since 2019. JavaScript makes use of a package manager, npm, npm is a package manager for the JavaScript programming language. It is young and it is recognized by top companies in the world including Microsoft, Trello, Netflix, eBay, Medium etc. We can say Node.js is a framework and the main back-end framework in JavaScript.

Definition of PHP

PHP is a popular general-purpose scripting language that is especially suited to web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1994; the PHP reference implementation is now produced by The PHP Group. PHP originally stood for Personal Home Page, but it now stands for the recursive initialism PHP: Hypertext Preprocessor. PHP code is usually processed on a web server by a PHP interpreter implemented as a module, a daemon or as a Common Gateway Interface (CGI) executable. On a web server, the result of the interpreted and executed PHP code – which may be any type of data, such as generated HTML or binary image data – would form the whole or part of a HTTP response. It is actually processed by an interpreter called The Zend Engine. And now it is used on more than 85% of web servers that is installed on web servers such as Apache and Nginx

Benefits Of PHP

PHP's environment is easier to setup or configure: In PHP, it is very easy to setup a server and start using it's PHP's environment. For example, it is really to setup PHP in servers like WAMP (Windows, Apache, MySQL, and PHP) or XAMPP (Cross-Platform).
Wamp Installation

  • Ease to use: Any individual who is new to programming can easily learn to used them within a short period of time. The syntax for programming in the hypertext preprocessor is quite similar to the C programming language. So the individual who is familiar with C can easily create PHP website scripts.

  • All internet hosting services support: PHP supports most hosting services like Hostinger, Bluehost etc PHP is widely used in hosting because Node.JS is a type of language that is hosted by some services, so people use PHP because of this.

  • PHP works with most CMS (Custom Management System) and supports frameworks: PHP is a language that works with CMS technologies like WordPress, Blogger etc. It also support well-known frameworks like Laravel too.

  • CMS such as WordPress, Drupal, Blogger, etc, even if there is only a part of your project includes these technologies (for blog, home page admin page, online store etc.): This pages come along with the CMS technology and you can just add an article on a particular page with CMS.

  • Using a relational database is supported in PHP.

Here are the benefits of Node.js:

  • It is scalable: Node.js scalability is one of the core benefits for those startups that are looking to grow. Being a lean technology tool, it fits well for microservices architecture, which in turn ensures better flexibility. With Node js programming language the app logic is broken into smaller modules, thus developers can easily add more modules to the existing ones, instead of integrating new features with the large monolithic core.

  • Built-in server support: Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine.

  • Node has an almost identical syntax to JavaScript, so it easy to pick-up and learn for JS developers.

Where Php Wins

PHP has just won in different cases as most PHP developers are looking for a technology which is easier to use. PHP is a very interesting language and even when you see the basic syntax, you will find it easy and know how easy the language is. PHP has been used in over 290,000 open source projects and it has been used in the creation of top companies in the world. PHP is the main back-end programming language which has a relational database and works with a server and then you run it on localhost in your web browser. PHP has been used in creating CMS technologies like Joomla, WordPress and even Drupal. PHP is open source too and large amounts of Database can be stored in PHP, in the latest version of PHP, it has extended support for Object Oriented Programming. It also has an improved support for MySQL with new extensions.

Where Node.JS wins

Node JS won in just few cases here. Node.JS is very fast, take a look at the performance tests by toptal.com, comparing how GO, PHP, Java, and Node.JS handle concurrent requests. There a couple of reasons for Node.JS showing such result. Node.JS is used as a single programming language as Node.JS doesn't work for back-end only, it works for front-end development too. It enables programmers with the ability to write code for the server-side applications using JavaScript. They do not need to use other server-side programming languages. Thus, application deployment becomes easier for the developers as almost all browsers support JavaScript Node.JS is a mobile friendly framework too.

PHP Testing
Let's go for the testing, here is the real Node.js vs PHP so we just have to test both sides, the testing here has been done by most evelopers and it has also been published in many articles. So, let's go for PHP:

PHP testing

Node.js testing
We've done the PHP testing so we have to do Node.js testing too. We have to go for Node and check the results.
Node.js testing

In the testing, PHP was even faster than Node.js in reading the file, filing array, MySQL Query etc.

In conclusion, PHP is better than Node.JS, PHP is easier to use but Node.JS is easier too learn and PHP has lots of features compared to Node.JS and most developers loves PHP than Node.JS. Node.JS is seen as the future of JavaScript and all developers should have a re-think as a programming language cannot be compared to a framework.

Top comments (16)

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