DEV Community

loading...
Cover image for PHP vs Node.js

PHP vs Node.js

phamn23 profile image Nathan Pham ・3 min read

Web development is constantly changing. In order to stay relevant, programmers must analyze trends and learn the next "big" technology stack. The debate between PHP and Node.js is fairly old news. That being said, let's review some of the pros and cons of using either language from a developer standpoint.

Background

Node.js is rapidly growing and was quickly adopted by Netflix and Walmart. By contrast, PHP is slowly declining. However, the language is still being widely used due to the popularity of PHP content manager systems like Drupal and Wordpress. Surveys suggest that nearly 80% of today's websites were created with PHP, whereas a mere 4% were created with Node.js.

What is PHP?

No, PHP is not a dinosaur that went extinct (although that was a good guess). PHP is a scripting language well suited for web development because you can embed server-side code directly into HTML. PHP features a loose typing system and object-oriented features (classes). Here's what "Hello World" looks like in PHP.

<?php
    echo "Hello World",
?>
Enter fullscreen mode Exit fullscreen mode

What is Node.js?

Node.js is a JavaScript runtime environment. Running on Google's high performance V8 engine, Node.js gave client side JavaScript the power of a standalone application capable of running servers. Node.js is single-threaded, asynchronous, and very scalable. Here's what "Hello World" looks like in Node.js.

require("http").createServer((req, res) => res.end("Hello World")).listen(8080);
Enter fullscreen mode Exit fullscreen mode

Comparing Development Environments

Neither PHP nor Node.js is difficult to set up on a local environment. Both can be integrated with VS Code or another editor. With XAMPP, setting up PHP servers are relatively painless. Just click a few buttons to enable Apache and MySQL and you have a fully featured server running on localhost. Building servers from scratch in Node.js takes far more effort, but half of the language's power comes from frameworks like Express anyways.

Advantages of PHP

Compatibility

With many different server and platform options to chose from, PHP can literally run on any configuration or operating system necessary for hosting a website.

Stability

PHP has been around for a while and is still actively being developed. Programmers are continuously updating and improving the stability and security of PHP. This is not to say Node.js doesn't have its own community of developers - just that PHP has been around much longer.

Easy Use

Developers without much technical background in PHP could probably make an entire blog or e-commerce websites because of the built-in functions and integration with MySQL. Node.js can make MySQL connections, but production apps often abstract queries behind an API layer (which can take longer to set up).

Disadvantages of PHP

Modularity

The possibility of integrating HTML with a server makes it difficult to update or extend existing applications. With the help of powerful frameworks, JavaScript programs rarely face these problems.

Obselete Model

PHP's age is as much as a bonus as it is a disadvantage. Traditional techniques of HTML rendering and creating database connections are slow and rigid. Node.js is pioneering new rendering models, such as the JAM stack with serverless functions.

Advantages of Node.js

Low Learning Curve

JavaScript is an extremely popular programming language and is necessary to provide complex user interactions on web applications. Node.js is essentially an extension of JavaScript. Nearly all features supported by modern browsers (ie: ES6) are also compatible in Node.js. Once you know client side development with JS, it is much easier to become a full-stack engineer by learning Node.js rather than learning an entire set of technologies that can be paired with PHP.

Flexible

There is no set programming style that should be used when developing Node.js applications. In addition, the large range of available modules shipped with NPM makes developing applications easier to implement.

Fast

The V8 engine and asynchronous nature of JS allows Node.js to handle far more requests than PHP can in a shorter amount of time.

Disadvantages of Node.js

CPU

Node.js is single threaded, making it less than ideal when handling graphics, audio, or video.

Immature

Node.js hasn't been exposed to the market for that long in comparison to PHP. In addition, many NPM modules are untested or unstable, creating security loopholes that are difficult to eliminate when developing a production application.

Closing Thoughts

It is difficult to say whether PHP or Node.js is better than the other. In fact, it really depends on the type of application you are trying to build. For example, it would be much easier to build a Single Page Application (SPA) with Node.js because frameworks exist to simplify the process. However, you may find that it is easier to deploy PHP applications simply because it is supported on nearly every server.

Discussion (22)

pic
Editor guide
Collapse
ajayyadavexpo profile image
Ajay Yadav

80% and 4% still you are comparing... It is easy to find a JavaScript developer who hates php a lot, don't know why may be jealousy.... But php community welcome JavaScript developers and uses JavaScript (Vuejs, reactjs and JQuery ) with php.....

I believe php community is more friendly than JavaScript community 😏

Collapse
jcarlosweb profile image
Carlos Campos

php -S localhost:8000 -t out/

Collapse
kmistele profile image
Kyle Mistele

Great article! Couple thoughts on this -
The “hello world” comparison isn’t quite fair, since for PHP you’re assuming you have a static file server like Apache installed and configured to actually serve the PHP file. The node code creates a server and serves the response, so the two aren’t quite analogous. PHP can also create a lot of security problems if it’s not used right - things like file includes and SQL query generation can go really wrong really easily if not done right.

I’m probably a bit biased though - I’m definitely more of a Node.js fan.

Collapse
ajayyadavexpo profile image
Ajay Yadav

Php requires apache to run, and node also requires nodejs pre-installed....

I can see your jealousy level 😜

Collapse
noclat profile image
Nicolas Torres

PHP does not require Apache to run. You can execute scripts in command line the same way than Node.js.

Thread Thread
kmistele profile image
Kyle Mistele

True, you can definitely install just an executable

Collapse
jcarlosweb profile image
Carlos Campos

php -S localhost:8000 -t out/

Collapse
back2lobby profile image
8ack2Lobby

No, he is not assuming any kind of complex apache configuration for that hello world code. PHP has its own built-in server. Its just a simple command "php -S localhost:8000" and thats it. Simple!

Collapse
tanmaymishu profile image
Tanmay Das

Which is simpler than that of NodeJS ;-)

const http = require('http');

const requestListener = ...

const server = http.createServer(requestListener);
server.listen(8080);

Collapse
sesha profile image
Sesha

Talking about your PHP disadvantage list, did you try Laravel blade at least once?

Collapse
phamn23 profile image
Nathan Pham Author

Unfortunately no (I've gotten into React to far). I'll try it out though and see how it goes.

Collapse
alexgwartney profile image
Alex Gwartney

You also can mix libarys like vue and blade together with php as well.

Collapse
faks profile image
Oskars Germovs

I am very sorry, to hear this but this is very biased and very far from reality.

Collapse
otengkwame profile image
Oteng Kwame

Why does some developers compare PHP with Node.js. NodeJS is not a programming language but a runtime environment for JavaScript, It is just a technology for making JavaScript a server side language. I think this should be understood. So the comparison should not be at all.

PHP has a built-in server to run any PHP app. It is easy to work with PHP than NodeJS because NodeJS depends on a lot of packages to work based on the node_modules directory.

Collapse
xicarus profile image
Sergiu-Lucian Petrica

Exactly this, such comparisons are borderline ignorant. Compare Node to ReactPHP if you wanna make an article.
I won't even begin to mention how biased the article is.

Collapse
koas profile image
Koas

As far as I'm concerned, it's not PHP vs Node but PHP and Node.

Programmers that have been using PHP for years can benefit of Node in many ways. I personally use it for PDF generation (using Puppeteer) and as a WebSockets server.

I've never used Node for anything else, but from what I've read Node is still a bit far from what frameworks like Laravel (or Rails or Django) can give you out of the box. That may have changed or will change in the future though.

Today a lot of logic has moved to the front, and JS has become a very popular language, so for new programmers it may make sense to learn JS and develop their server side logic with Node. People beginning their career in web development these days are already overwhelmed with lots of things to learn, so if you can reuse your language knowledge to write the server logic it's a win.

But I still think that some tools are best suited for some tasks and, from what I've read and my own Node experience, I don't think Node is as good as PHP for a big size web apps. Frameworks like Laravel are mature, well documented and give you many features out of the box. To achieve the same results in Node you would have to write a lot more code and use many modules.

So, why not use the best tool for the job?

If you know PHP (or Ruby or Python) I think you should write your server side logic in PHP, and use Node for tasks that are far easier to do in Node.

If you only know JS go ahead and write your server side logic in Node. But if you have the time I'd recommend you to look for server side frameworks in other languages that may make things easier for you.

Collapse
xicarus profile image
Sergiu-Lucian Petrica

Node is a runtime environment while PHP is a language, you're comparing apples to oranges. If you paid attention to the snippets you posted you would've figured this out by how many 'extras' the Node snippet contains.
If you want to compare Node to something compare it to ReactPHP or Swoole, then see how your reqs/s argument holds up.

Also, nobody who's sane is writing PHP in the view layer anymore, and it's been a very long time since this was the norm. This tells me you're not familiar with PHP development at all.

Don't take this the wrong way, but you should do your research better before writing such articles.

Collapse
kalashin1 profile image
Kinanee Samson

Stop making posts like this. The both languages offer similar features albeit a few exceptions. Anything you can build with php, you can build with nodejs. It just which one you are comfortable with. Although if you are new to sever side, I would advice you to go for node js

Collapse
rsvp profile image
Аqua 🜉іtæ ℠

&& what about php-apache-module-speedup vs pure node? JS scripts can be handled both @ private dark side of backend || via public frontend browser capabilities. Unlike backend-only php, So, can u be so bold to mishap someone private Node's life via JS @ frontend?

Collapse
ahamedsajeeth profile image
Collapse
tiiunder profile image
Felix G

I'm pretty happy I don't have to use JavaScript in the backend, it's terrible in the frontend already ;-)

Collapse
abdurrkhalid333 profile image
Abdur Rehman Khalid

Great Article, I do have some insecurities but others have described them below, so I will not repeat them again. Keep writing, that was a really good article 💕🌺