Both Node.js and PHP are popular platforms for websites, APIs, and other types of web content. They have a few similarities, but their differences far outweigh those similarities. In this post, we’ll take a look at how they stack up against each other.
It’s Node.js vs. PHP… let’s get ready for the showdown!
First, let’s take a deeper look at how Node.js and PHP are similar. Since they’re both common in software development, we can look at a few aspects of their similarities. We’ll see what similar types of applications they handle, the platforms they run on, and how they’re both extensible. Let’s start with the common application types.
Node.js and PHP are both used to serve web requests. They run on a server and handle requests that are routed to them. You can use either one to serve static content, dynamic web pages, and requests for data.
Node.js is a more popular option when it comes to serving web sockets—the go-to implementation is socket.io. But PHP has had sockets available for quite some time. You could have written a socket server or client in PHP in 2003. Since web sockets have become widely available, Ratchet gets you into web sockets quickly in PHP.
Both technologies can be used for serving up streams as well. However, Node.js has a very straightforward way of doing so with the stream API. PHP is a bit more complicated; you’ve got to do more of the low-level coding in order to get it to do something like streaming a video.
Where Node.js and PHP overlap, I’m going to give this one to Node.js for keeping things a bit more user-friendly. You generally don’t have to get too low-level to write modern web applications with Node.js. This translates to better productivity.
Node.js and PHP are both cross-platform. You can run Node.js on Linux, macOS, Windows, or even SunOS; you can target 32-bit, 64-bit, and ARM processor architectures. And, of course, there are official Docker images on Docker Hub. If compiling the source code is your thing, you can always do that, too.
PHP is available for Unix-based operating systems (including Linux and Sun), macOS, and Windows. Usually, you’re going to install PHP as a web server module or as a CGI component. You can run PHP directly from the command line—as some folks do—but generally, it’ll be run through your web server. There are pre-built packages for PHP, or you can compile it from source code.
So we have a tie when it comes to compatibility with platforms. Both Node.js and PHP can run on almost any platform.
You might want to extend these technologies to fit your own circumstances. You can always fork the source code, modify it, and compile to extend. But there are also add-ons and package managers for both platforms, which we’ll talk about more a bit later. For now, it’s important to note that Node.js and PHP can be extended. They’re open source, and there is a rich ecosystem of add-ons from which to draw.
Node.js has a rich package registry at npmjs.com with strong community contributions. There are other Node.js package registries, but none are really in the forefront yet—although there are some resources about contenders on node-modules.io.
PHP has two popular package managers: PEAR and Composer. The Composer packages are listed on packagist.org. PHP has been around longer, so the community has had more time to build alternatives. We may see more options for Node.js coming to the forefront in the near future.
In terms of extensibility, we effectively have another tie. If you can code in C or C++, then you can extend. But you don’t even need that; you can install packages. If you don’t mind getting into matters of trust, you can also find pre-packaged versions that have already been extended.
I’m mentioning performance here knowing it’s a loaded topic. After all, the bottom-line performance of an application depends on a lot of factors. Programming, environment, performance of dependencies such as databases, and usage will impact performance. Since Node.js and PHP generally aren’t used in the same way, it’s going to be tough to compare performance directly (some have tried and just look at the backlash in the comments).
Node.js will typically be used to serve up API requests, so it’s usually just a thin layer between a web client and a database. On the other hand, you might use PHP for the whole stack including the compilation and rendering of a webpage.
To avoid comparing apples to oranges, we have to consider many factors. It might not even be worth the effort. After all, your environment will behave differently than any benchmark. The best way to understand performance is to monitor it with something like Retrace. This way, you can keep your Node.js or PHP application performing under real-world circumstances—where performance really counts!
This one is a wash with no points awarded on either side.
PHP and Node.js are probably more different than similar. If you look at the most popular websites in the world, most of them use more than one back-end language. PHP is among the most widely used. Know what isn’t? Node.js, that’s what. In fact, it isn’t even listed as a back-end language for any of the top websites.
There’s a reason for that.
Runtime vs. language
PHP is a scripting language. Theoretically, you could run it in a browser if you had an interpreter. However, PHP was made to deliver web content to browsers from a server. It has to be interpreted by the Zend Engine in a web server module or a CGI.
Node.js is the winner here since it acts on its own. PHP relies on a separate web server and doesn’t really do anything that can’t be done by other popular languages.
Node.js really has come a long in way in terms of its applications. For a while, it was primarily used to serve web requests using something like Express. It’s also popular for communicating over sockets via socket.io. But nowadays, it’s used in so many other ways, such as in building web applications using popular build tools like grunt, gulp, or webpack. These build engines are responsible for compiling and optimizing all the front-end code in so many applications today. Plus, desktop applications such as Skype, Visual Studio Code, and Slack are being built on Node.js. And those are only a few of the most notable that use Electron alone.
PHP is primarily used to serve web applications. But don’t be fooled! It can be used to do almost anything since you can execute it directly via the CLI. You can even use it to build basic desktop apps with PHP-GTK or perhaps PHP-Desktop. Even still, those haven’t had nearly as much success as Node.js desktop apps.
In terms of desktop applications, Node.js is clearly the winner.
Node.js is written in C++. Its GitHub has 25,000 commits, 131 branches, 522 releases, and 2,379 contributors as of this writing. There are 504 open issues and 222 pull requests. These are all tracked in one place—the public GitHub repo.
PHP is written in C. Its GitHub is a mirror of the official PHP git. It has 109,885 commits, 287 branches, 940 releases, and only 580 contributors. It’s a bit tougher with PHP to get to all the issues and feature requests since the feature requests are on php.net rather than on GitHub. There are 444 bugs and who knows how many RFCs; they don’t give you summaries on that page.
I’m giving PHP the win here since their numbers are better. They’ve been around a lot longer; you can probably attribute the win to that. But I have to hand it to Node.js for having better organization. It’s easier to get to the information as it’s all in one place and they’ve got fairly comprehensive documentation for contributors.
When it comes to non-blocking, reactive request handling, Node.js is your ticket. It was one of the first to do this—and it did it by default. It’s possible with PHP, too. However, you have to use an add-on like ReactPHP, Amp, or spatie/async to accomplish your goal. This means you have to adjust your programming model to the framework. Although you might find some pros in this, the cons stack up against PHP on this one. Node.js clobbers it by being first and by doing it natively.
In the end, the win goes to Node.js. But the race was very close, so let’s see if we can push it just a bit further…
On StackOverflow, at the time of this writing, there are 244 jobs postings for Node.js compared to 153 for PHP. There are also 254,303 questions tagged with Node.js versus a whopping 1,248,725 questions tagged with PHP.
I don’t know if more tagged questions is a good or bad thing, but PHP has had more time to accumulate questions. Let’s presume more questions means more problems for users. If that’s the case, this can be an indication that PHP is harder to use. In light of this, I’m going to call the game for Node.js since it seems to be more in demand—and that’s definitely a good thing!
Whether you’re using Node.js or PHP, you can still use Retrace to diagnose, improve, and accelerate performance! It takes minimal effort to start collecting APM metrics from PHP and Node.js using lightweight profiling modules. Besides, the Retrace dashboard is super-intuitive!
Head over to the demo sandbox and see for yourself.
- A Detailed Guide to PHP Debugging - January 31, 2019
- Node.js Logging Tutorial - January 18, 2019
- Comparison: Node.js vs. PHP - January 4, 2019
- ELMAH Is Dead. Get More Detailed Exceptions With Retrace - November 30, 2018
- Improve MySQL Performance With This Tutorial - November 27, 2018