5 ways how PHP is better than Node.js

April 24, 2012 – 5:32 pm Tags: ,

All hail Node.js! Boo PHP!

Except there are various things where PHP is better than Node…

Be sure to read the comments too for some discussion :)

Easier to find hosting

Majority of the web’s hosting providers are able to provide PHP hosting.

For Node, you need a much more specialized hosting provider. Typically you need shell access to set up your application, and most web hosting services do not include anything like this, or if they do, the other packages are much cheaper.

It’s easier to get started with PHP

With PHP, you can simply install WAMP, LAMP or MAMP and off you go. For deploying code into a web host, you just drop your files there and you’re done.

Although Node itself is not hard to install, it does require more specialized knowledge to set it up nicely. In order to install it on a server, you generally need some level of Linux sysadmin knowledge in order to set up the necessary packages, and tools such as process monitoring to keep your node instance running in case of a crash.

If your PHP code breaks, it doesn’t bring your whole server down

As PHP code runs each in its own process and the web server sits in front and directs this, if one of the requests causes an error it will only affect that specific request.

In a Node environment, you typically have a single process serve all the requests. If one request causes an uncaught error, the entire server comes down.

PHP processes are short lived

In PHP each process is only alive for the duration of the request. This means you don’t need to worry as much about resource allocation and memory.

In Node as the process is running for a long time, you need to be careful to manage memory properly. For example, it’s easy to accidentally take tons of memory if you forget to remove items from a global array. It’s also easy to accidentally write code which leaks memory. (which in turn can bring the whole process down again)

Bigger standard library

PHP’s standard library is much bigger than Node’s.

In closing

This is not to say that PHP is better in every way than Node. It’s simply to point out that while Node is very good at certain things (real time processing of messages for example), it is not a silver bullet for all problems.

Feel free to leave your thoughts in the comments, or if you can think of more ways where PHP is better than node.

Share this:
  1. 25 Responses to “5 ways how PHP is better than Node.js”

  2. Some of those can be turned around, though.

    Hosting – You only need 1 host. You don’t need to find 30. But Node hosting tends to have better features, so when you do find one, it’ll probably be a good one.

    Code Breakage – Nobody will get away with writing crappy code. Any problems will be more obvious and require attention, instead of getting ignored.

    - Professional PHP Programmer.

    By WC on Apr 24, 2012

  3. Apart from the single process stuff, your reasons seem to boil down to stuff caused by PHP being an older and more popular language. Just saying.

    If anything I’m suddenly interested in trying it. I couldn’t care less before because I’ve already got PHP. But if PHP’s maturity and popularity are the only good reasons why I shouldn’t try Node.js, it has to be good. Are you secretly luring people away from PHP? ;P

    By DrLaunch on Apr 24, 2012

  4. @WC, true, it does go that way too. And yeah, crappy code is never good, but it’s easy to forget some minor parameter validation on some rarely used page. Then someone inputs something stupid that nobody sane would ever try inputting, and bang, your app crashes entirely.

    @DrLaunch I think what actually is luring people away from PHP is that it is indeed starting to show its age as you said :)

    To give a more practical example of how PHP’s strengths combine, I would dare say that if you have a slightly simpler application which relies mostly on page loads or basic ajax, you may be better off writing it in PHP.

    You won’t reap any big benefits from using Node in a case like that, and you will just need to worry more about things like handling all error scenarios correctly, making sure you don’t introduce memory leaks, making sure your node process will get restarted when a bug actually slips by and causes a crash…

    When someone implements a higher level web serving facility in Node which addresses these issues, well, then these points probably become irrelevant :)

    By Jani Hartikainen on Apr 25, 2012

  5. Being able to run in synchronous mode – no call back hell. =P

    I love Node and PHP, but there are things in Node that just urk me.

    By EllisGL on Apr 25, 2012

  6. I wouldn’t try to pen them once against the other because in my opinion/experience they don’t serve the same purposes. If I need a website I’ll use PHP, if I need custom http proxy server I’ll use nodejs. I think nodejs’s real strengths can only compliment a project developed in PHP (and others) but certain not (yet) replace it.

    Being able to create a very fast http server in only a dozen lines of js is where nodejs shines bright. For everything else, there’s PHP ;)

    By smassey on Apr 25, 2012

  7. “It’s easier to get started with PHP

    With PHP, you can simply install WAMP, LAMP or MAMP……”

    Add ASCOOS WEB SERVER

    By AWS on Apr 25, 2012

  8. I wouldn’t say that using node requires linux sysadmin knowledge. I wouldn’t consider myself a sysadmin but I’ve managed to get node installed/running smoothly in my linux machine. I will say that you need to by fairly comfortable in linux to get everything started though.

    By Angelo R on Apr 25, 2012

  9. I’ve been waiting for posts like this to show up. Node is indeed nice for http proxies. It is also very nice for websockets. I think the websockets are the real danger to php.

    In my experience in the past any long running php process is doomed to huge memory consumption. Granted I haven’t taken a close look lately and I’ve also heard/read that php 5.4 is supposed to have some real memory optimizations. At any rate, php will need to find a way to make a websocket server that isn’t a memory hog.

    The other thing that concerns me is code reuse. If you write your site in php and then turn around and have to implement an api for it in nodejs for websockets, that sounds rather duplicative :/ Also there are many javascript frameworks (like ember.js) that are becoming popular, they also move towards an api-like interaction with the server. If you write most of the site in javascript, it becomes really tempting to use those same libraries on the server, which may keep things more DRY.

    I’m more confident with php than any of the other languages I’ve toyed with, but in my opinion php will have a real challenge from node. Granted the hosting situation isn’t likely to change for a while, but if there is enough demand; node hosting for the masses will happen at some point.

    By sitedyno on Apr 25, 2012

  10. @EllisGL good point, asynchronous code can sometimes make the code more complicated to follow. Libraries like async do help with that, although that just shows it is a problem.

    @smassey Yep they both serve their own purposes, that is pretty much what I wanted to point out :)

    @Angelo R It’s true getting a dev setup running is not that hard, but if you want to have a stable production environment then it’s a bit trickier, as you need to set up things for logging and process monitoring.

    By Jani Hartikainen on Apr 27, 2012

  11. I’ve just developed a restful api using node.js and I found it good at what it touts, but I ran in to some big issues that made me believe the larger the implementation the less likely I will use javascript as the language of choice. The prototype model is terrible.. for what we do. OOP and DDD have taught us how to create a rich domain model enforced by constraints that arguably requires the language to have an actual OO model with some sort of typing. To me the prototypical model of js and v8 makes enforcement of the anti-corruption layer ugggly and very lengthy. I do love what it is good for, the asynchronous model is perfect for handling HTTP transactions quickly but it can be pretty annoying designing work arounds for common tasks so they can be executed correctly. Using asynch and coffee-script etc. are good solutions for structuring code, but to another developer it’s hell. Why make them jump through the hoops of learning a meta-language and control flow libraries just so they can interpret what your code is doing, this is not the same as using a framework with php… unless that framework completely re-formats the language. My take, from using node.js (express, mocha etc) and php (vanilla, zf 1/2.0, symfony2, silex etc)server-side and backbone.js, jQuery on the client side is that I would much rather have a rich, sane, easy to understand domain model on the server side and if the time comes to switch to node or some other means of speeding up your app… you’ll know it. That isn’t to say node doesn’t deserve it’s own right in smaller, straightforward situations.

    By Jowee on Apr 27, 2012

  12. This article is just for trolling, right?

    By mnt on Aug 8, 2012

  13. I can easily counter argument all your points:

    Easier to find hosting: Does not make Javascript any worse than PHP.

    Its easier to get started with PHP: Does not make Javascript any worse than PHP.

    If your PHP code breaks, it doesn’t bring your whole server down: A fatal error or unhandled exception will bring PHP down, what you talk about here is about an underlying server like Apache managing processes breaks gracefully. There are many solutions that will do that for NodeJS applications.

    PHP processes are short lived: You are talking about a one-process per request model that is commonly used with servers like Apache. Being short lived doesn’t mean that they will be less memory consuming or such, actually, one process per request can blow up your CPU and RAM on intensive web-services. In Javascript, specifically the V8 VM that NodeJS uses, there is a garbage collector that will detect what parts of the memory to free.

    Bigger standard library: What is the evidence? and if so, how does that make Javascript a worse language?

    …considering that your post is serious.

    Note: NodeJS is not a language, it is a general purpose server that runs Javascript.

    By Mario on Aug 29, 2012

  14. Mario,

    Yes this was a comparison with Node, not JS itself, as you need things like Node on top of JS to build stuff that you can build with PHP (specifically web applications in context of this post).

    Points 1, 2 and 3 all make for easier time when setting up a system.

    As for point 4, have you built a large scale Node application? I have, and it can be hard to make it completely memory leak free, as there are no tools to help with debugging memleaks. A typical PHP setup with Apache or such does not have this problem at all.

    Point 5, it should be pretty obvious. Look in the PHP manual, now look in the builtin NodeJS modules. (Yes I know, npm is awesome and has lots of stuff, but that’s not the standard library ;) )

    This was at least a half-serious post. I’ve seen lots of people tout node as a magic bullet to all problems, which it isn’t :)

    By Jani Hartikainen on Aug 30, 2012

  15. Sorry but this article is just crap. All the statements are false.

    By Anonymous on Sep 11, 2012

  16. Jani, well… it is fine that in your opinion points 1, 2 and 3 are that important for you.

    For point 4, I have developed both, large scale NodeJS and PHP servers. No problem with any so far… the thing is that I find NodeJS more elegant.

    You say that a typical Apache+PHP application won’t have those problems (the ones you mention) at all. What problems? Why? Any proof, evidence or sample-code to reproduce such problems?

    Point 5, I think that the core of NodeJS is so good designed that such a standard library is not needed at all, or yet. Please demonstrate your statement by providing some examples on what NodeJS lacks by not having such a library.

    I do not say that NodeJS is a “magic” bullet. I am just starting to like it more than Apache+PHP.

    By Mario on Sep 11, 2012

  17. Well, one example for 4 is obviously an array where you collect things, yet forget to clear it. Or simply an array where you forget to have a `var` keyword in front. It’s also possible to do this by having event listeners on an object which you then discard, without removing the event listeners, which can in certain circumstances leave the event listeners hanging in memory (such as if they have circular references)

    Point 5 is easy. Imagine you don’t have npm and can’t install any libs. Now you can’t do almost anything out of the box.

    Obviously that’s a bit silly example. npm is great and the libraries for node essentially make up for the lack of a large standard library.

    By Jani Hartikainen on Sep 11, 2012

  18. The first example you give, for point 4, will affect all languages. Also, consider that the Javascript garbage-collector will free elements that are no longer needed, referenced. If you find you are constantly adding elements to an array and killing your RAM in Javascript, blame yourself as a programmed since no programming language, no matter how well designed it is, will ever fix a broken code for you.
    Second example, about the var keyword, is the behavior of Javascript. If you do such mistakes, better don’t try to code in Javascript.
    Next example, the one about event listeners, garbage-collector will detect freed elements. If you find yourself having circular references, you are a bad programmer, circular references can happen in PHP too.

    Point 5, what are those things that I would not be able to do out of the box?
    My example, imagine that the PHP standard library is not installed and you have no administrator access to the system where your code is hosted at.
    Now, remember that NPM can be run by normal users.

    By Mario on Sep 11, 2012

  19. Sure, memory management issues affect all languages. However, the effect it has on PHP is very minor, since PHP is so short lived – one request, and poof, even if there was a memleak, it’s all gone. You don’t need to make sure to clean everything up.

    Forgetting things like clearing arrays, circular references or var statements have nothing to do with being a bad programmer. Mistakes happen, or are you saying you never wrote code which had bugs?

    The thing with #5 is that you can’t have PHP without the standard library – that’s why it’s called the standard library. In Node, you essentially get the very bare low level stuff for sockets, http, events and such. No encryption, no database, etc.

    By Jani Hartikainen on Sep 12, 2012

  20. Well, I don’t think that considering PHP as a short-lived (which actually is not, as I pointed out in my first reply) proccess justifies “taking less care” of memory leaks. Have one thousand of requests that involve a memory leaking script and poof for the Apache+PHP server too~

    Well, mistakes happen, but you certainly are a bad programmer if you are “afraid” of a programming language by thinking that it will not handle memory issues for you.
    Still… as I have pointed before, the V8 VM has a garbage collector, which will free whatever is no longer needed. If you find yourself leaking memory by referencing needless elements and making them big, no programming language shall ever help you, not even PHP, and you will eventually explode your server with a thousand of short-lived requests involving a bad designed script.

    For encryption, database drivers and stuff you still need external tools to be installed, available and correctly linked to PHP. It is not everything out of the box.

    All that said, I am not saying that PHP is worse, it is just another language.

    By Mario on Sep 12, 2012

  21. If you want to make a entire website, yeah, php will be better for this, but create websites is not Node.js’ purpose: but it’s create chats, mmorpgs and applications that need people interaction in real time.

    And more: if you create a web application and you want to make it to be hybrid (half php, half node.js) you can do that, because node.js and php works in fully different context (it’s not like an hybrid of php and asp)

    By Ricardo Azzi Silva on Jul 4, 2013

  22. I have to agree with Mario on that discussion. I can’t really bring myself to accept that PHP is better than node for reason #4. You are saying that PHP is short lived, therefore if you forget to take care of memory leaks, it doesn’t have as much of an effect as it does on node. That’s a problem that involves the programmer and how it is coded, not the language itself. It also sheds light on the habits/skill of the programmer. I wouldn’t say C++ is a BETTER language than C because it handles garbage. If you handle garbage in C, it’ll work just fine. It’s like saying an automatic car is better than a manual cause if you accelerate without remembering to change gears, you might blow the motor in a manual, but not in an automatic. no!

    By Kevin on Jul 17, 2013

  23. I wrote this blog post a little while back on some unknown gems in PHP (the darker side).

    I also mentioned about the new built in PHP web server… Maybe that will be how PHP will do web sockets?

    Anyway, you can say that 4 of the 5 points are all points that node doesn’t have.

    https://www.coderprofile.com/coder/VBAssassin/blog/31/the-darker-side-of-php

    Kind regards,
    Scott

    By Scott Thompson on Jan 3, 2014

  1. 3 Trackback(s)

  2. Apr 27, 2012: PHP 优于 Node.js 的五大理由 | 探索---不思
  3. Apr 28, 2012: PHP 优于 Node.js 的五大理由 | 全信息-互联网深度阅读体验
  4. May 8, 2012: PHP优于Node.js的五大理由 - AdoPhper - 每一点进步,都是我们通往成功道路上的坚实脚印

Post a Comment

You can use some HTML (a, em, strong, etc.). If you want to post code, use <pre lang="PHP">code here</pre> (you can replace PHP with the language you are posting)