📜 ⬆️ ⬇️

PHP vs Node.js

This is a translation of Craig Buckler’s “SitePoint Smackdown: PHP vs Node.js” article and the court decisions of Craig Buckler’s Bruno Škvorts and James Hibbard.

From translator


There are many articles on this topic, with qualitative characteristic analyzes and performance forecasts. However, I still wanted to find an article where professional programmers share their opinions and try to tell something, to warn a beginner who is just learning PHP or NodeJS (server-side JavaScript). Many do not like PHP and say that PHP was created to die (yes, there is a garbage collector that kills our variables after executing the script, and not because Rasmus Lerdorf abandoned it), however, that now you don’t learn PHP at all, while while it works 80% of sites on the Internet. Therefore, if we want to be professionals and try to somehow expand our horizons, we need to clearly separate the PHP tasks and the NodeJS tasks, and not merge everything into a heap.

"10 rounds of boxers of different weight categories"


One fine day, Craig Buckler published a comparative analysis of PHP and NodeJS called “10 rounds” on SitePoint.com to determine who is the absolute champion. However, at the same time, he noted that this analysis is somewhat controversial. Therefore, for some entertainment, he invited two judges who would contribute to this boxing match. He asked Bruno Shkvorts (Bruno Škvorc, PHP column editor on SitePoint.com) and James Hibbard (James Hibbard, JavaScript column editor on SitePoint.com) to comment on each of the rounds, blow by blow - score.

Introduction


Web programming is rapidly evolving, and back-end developers face the question of choosing between established Java, C, Perl heavyweights and modern web-based languages ​​such as Ruby, Clojure, Go. Your choice is of great importance, imposing its imprint on the application.
')
But what choice to make for web development?

I do not want to start a holivar, but still we will talk today about PHP and NodeJS:


Why not talk about C #, Java, Ruby, Python, Perl, Erlang, C ++, Go, Dart, Scala, Haskell, and others?


Would you read an article about everything and everyone, do you need an encyclopedia? Therefore, we restricted and narrowed the circle to two famous ones because:

1. PHP and Node.js are web-based, both are open source, they are directed exclusively to web development.
2. PHP is an old language, however, Node.js in our case is an upstart, which is gaining momentum in popularity, so a php developer should ask. Is it worth changing technology?
3. Many developers program from far 90s to PHP and JavaScript, and not everyone wants to switch to other programming languages, because they don’t give them their due.

rules


The boxer in the right corner is PHP, the boxer in the left is Node.js. The absolute winner will be the technology that wins by the number of rounds.

Round One: Quick Start


In this round, we determine how quickly we can write the “Hello, world” page in a particular programming language, at which time we include the time spent setting up the server.

Prepare the environment:



1) How fast can you build a "Hello World" webpage in PHP:

<?php echo 'Hello World!'; ?> 

This code can be written anywhere, as a rule, the code is written in files ending in the .php extension. If you wrote this code in the index.php file and launched it on the local server via port 8000 (on your computer, and not on a real hosting), then its display will be available at:

 http://localhost:8000 
.
However, using a ready-made PHP interpreter with a built-in server is rather unreliable. It is better to use ready-made solutions, Apache builds - XAMPP or virtual OS (Vagrant). By the way, you can upload your file to any internet hosting.

2) Installing the Node.js platform is easy if you work on unix-like systems, you can do it with the help of package managers. Let's create an index.js page:

 const http = require('http'); const hostname = 'localhost'; const port = 8000; http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello World\n'); }).listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); }); 

We repeated similar actions, JavaScript code is usually written in files ending in the .js extension. If you wrote this code in the index.js file and launched it on the local server through port 8000 (on your computer, and not on a real hosting), then its display will also be available at the address (provided that php is now running on another port) :

 http://localhost:8000 

Now let's evaluate the code, even if you know client JavaScript well, you will have to think and understand what is written here. You have to understand closures and callback functions, promises, programming on the Node.js side requires some skill.

PHP is conceptually simpler and wins this round. Although PHP has several software dependencies, PHP is less difficult to learn.

Judges Score - PHP 1: 0 Node.js



Second round: help and support


In fact, you will not go anywhere on your knowledge without studying any courses, modern practices, in order to develop, you need to ask questions on forums such as StackOverflow. PHP wins this round easily, it has a great guide on php.net and twenty years of frequently asked questions. Whatever you do, someone faced this problem and tried to solve it for you.

Node.js has good documentation, but the technology is quite young, so the answers on the forums will be much less.

Judge Rating - PHP 2: 0 Node.js



Third Round: Language Syntax


Unlike some languages ​​and frameworks, PHP does not force you to work in a certain way, and grows with you. You can write your small programs on simple PHP4 functions, and they will be different in their beauty from MVC PHP 5+. However, in this case, your code can be chaotic, you will start writing the best code only with understanding of some things.

PHP syntax changed with the release of new versions, because of which the work was carried out on backward compatibility. You can easily migrate code from PHP4 to PHP5. However, as a consequence of this approach, a mess (porridge) has formed in PHP.

For example, how do you calculate the number of characters in a string?

- count ()? str_len ()? strlen ()? mb_strlen ()?

You will find several functions in the documentation for this, but in fact, everything is simple. In general, there are many functions in PHP that work in the same way, try writing a few lines of code without consulting a mentor.

 <?php $str = 'Hello world'; $size_1 = count($str); $size_2 = strlen($str); //   $len = [$size_1, $size_2]; print_r($len); ?> 

C Javascript is different:

  var len = ('Hello world').length; console.log(len); 

We see that JavaScript is relatively clear at the same time, with several major trends. Its object-prototype model attracts developers, and the syntax seems to be pretty light, but it’s not. You will find criticism about mathematical errors (0.1 + 0.2! = 0.3) and dynamic typing ('4' + 2 == '42' and '4' - 2 == 2). But these situations rarely cause problems, and all languages ​​have features.

PHP has many advantages, but Node.js wins for some reason:

1. JavaScript seems to be the most incomprehensible language in the world, but as soon as you grasp its concept, other languages ​​become cumbersome compared to it.
2. JavaScript looks smaller than PHP, you do not need to deal with the same UTF-8.
3. Full-stack developers can write JavaScript code on both the client side and the server side. You no longer need to switch between technologies.
4. Studying JavaScript, you would like to write more and more in this language, this cannot be said about PHP.

Judge Rating - PHP 2: 1 Node.js




Fourth Round: Developer Tools


Both technologies have a good choice of editors, IDEs, debuggers, validators, and other tools. Here you can draw a draw, but still Node.js has a great tool, npm is a package manager, with its help you can manage modules and dependencies.

PHP has its own package manager, developed under the influence of npm - Composer. However, if npm is built in by default, that composer will have to be built in independently. Thanks to npm, Gulp has become widespread, Grunt - build systems for front-end projects.

Judges Score - PHP 2: 2 Node.js




Fifth Round: Wednesday


Where can these technologies be used? How to deploy them? What platforms are supported? Web developers often need to create applications that are only related to the web, for example, developing an online service, data conversion scripts, etc.

In PHP, you can develop desktop applications or console utilities, but mostly PHP is needed on the server side and rarely goes beyond that.

A few years ago, JavaScript was used exclusively for the browser. With the advent of Node.js, you can write desktop and mobile applications, and you can also program microcontrollers. Node.js has extended the boundaries of JavaScript.

Judges Score - PHP 2: 3 Node.js




Sixth Round: Integration


Your development technologies will be limited, unless they can integrate with databases and drivers. PHP is strong in this area. The development has been for many years, and its system extensions allow direct work with any host using the API.

Node.js is catching up quickly, but you can pretty much sweat to find modern integration components for old stuff.

Judges Score - PHP 3: 4 Node.js



Round 7: Hosting and Deployment


How easy does a new application unfold on a real web server? Here is another clear PHP win. Any internet hosting supports PHP. You can get MySQL database at a bargain price. Here PHP is much simpler than the sandbox (local server) and you will be accurately notified which PHP extensions are disabled and which ones are not.

Node.js is a completely different beast, and it can work on the server side constantly, without breaking the connection. For this, you will have to look for specialized hosting. You need a virtual cloud (VDS / VPS, server environment, with full access). Unfortunately, not all hosters can afford this, so prices will be appropriate.

Judge Evaluation - PHP 4: 4 Node.js



Eighth Round: Performance


PHP does not slouch and there are real projects, and options that allow PHP to work faster. Even the most demanding PHP developer rarely cares about speed, but Node.js performance is usually better. Of course, performance is largely a result of experience and team development, however, Node.js has several advantages:

1. Fewer dependencies
All requests to a PHP application should be directed to a web server that runs the PHP interpreter, which processes the code and returns it. Node.js does not need so many dependencies, and although you are almost certainly using a framework on the server, such as express, it is quite lightweight and manages part of your application.

2. Fast interpreter
Node.js is smaller and faster than PHP. This is due to the legacy of Google, which made a huge contribution to the performance of the JavaScript engine - V8.

3. Applications work constantly
PHP performs the usual client-server model. Each page request initiates an application, loading database connection parameters, extracting information, and displaying HTML code. In Node.js, the application is constantly running and needs to be initialized only once. For example, you can create a single database connection object, which is reused in a new query. True, there are ways to implement this behavior in PHP using special systems such as memcached, but this is not a standard function of the language.

4. Event, non-blocking input / output stream
PHP and most other server languages ​​use an obvious blocking model. When you make a request to extract information from the database, the request will complete and complete the process before moving on to the next operator. Node.js is different. Node.js need not wait. Instead, you can create a callback function that, while listening to the process, is executed after the action completes.

Though Node.js applications are noticeably faster than PHP has its own pitfalls here.

Node.js / javascript runs in one thread, and most web servers are multi-threaded and process requests in parallel. Writing asynchronous code is difficult and carries its own problems.

Judges Score - PHP 4: 5 Node.js




Round nine: programming passion


It's a bit hard to compare, but relatively few PHP developers are passionate about the language itself. When was the last time you read an PHP article or watched a presentation that captivated the public? Perhaps all that was said? Maybe there is less fun? Maybe you do not look in the right places? There are some interesting features that have appeared quite recently, for example, the appearance - PHP7, but still this technology has been trampling for several years. This affected the language itself, many developers began to scold PHP.

JavaScript shares a community. There are those who love him and those who hate him, few developers sit on the fence. However, the responses on Node.js were largely positive and the technology is on the crest of a wave. This is partly because it is new, at the moment, Node.js wins this round.

Judge Evaluation - PHP 4: 6 Node.js



c :


It is not particularly important what language you use on the server side, it will still work, even if the project is abandoned. Many continue to use PHP. This is a safe bet and its support looks confident for another twenty years.
At the same time, we know that the Node.js climb was fast. The modern approach to development is evident in that it uses the same syntax as on the client side. JavaScript supports HTML5, web sockets. Node.js inevitably takes market share, but PHP, I doubt, will overtake. Both technologies have a great future. I declare this round to end in a draw.

Judges Score - PHP 5: 7 Node.js



Absolute winner


Final score 5: 7 in favor of Node.js. Node.js has a steep learning curve and is not ideal for novice developers, but it still wins. Simply.If you are a competent JavaScript programmer who loves his language, Node.js will not disappoint you. You will feel refreshed and gain a liberating web development experience. But do not dump PHP. PHP is alive, and there is little reason to tripping PHP, because it looks fashionable. PHP is easier to learn, you can learn professional programming techniques, the main practice. PHP is very easy to deploy on the server. Even obstinate Node.js developers should use PHP for simple sites and applications.

My advice: evaluate the options and choose a language based on your requirements. It is much more practical than relying on the pros and cons!

Source: https://habr.com/ru/post/273259/


All Articles