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:
PHP - was created in 1994 by Rasmus Lerdorf (Rasmus Lerdorf). He created a software shell (interpreter), which is installed as a module for the Apache or Nginx web server. Originally developed as a preprocessor of hypertext pages, so PHP can be easily integrated into HTML code, however, this approach is not a good practice now, but this approach was obvious for beginners. This contributed to the popularity of the language, so 80% of the websites on the Internet are written in PHP, and they, in particular, are running WordPress CMS (20% of the websites on the Internet).
Node.js - was created in 2009 by Ryan Dahl. He created a software platform based on JavaScript engine V8 from Google. Unusually, the platform has built-in libraries for processing requests and responses, thus you do not need to use a third-party web server and any other dependencies. Node.js is gaining momentum and is used by companies such as Microsoft, Yahoo, LinkedIn and PayPal.
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:
<?phpecho'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:
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
Bruno: PHP wins due to low entry threshold. There is more a question of perception. On the screen they are displayed equally and they have no significant difference. In fact, the syntax is much easier for newbies to programming.
James: When developing on a local machine, I don’t see the difference between them. To run the script in a browser, you need to have a web server and a terminal. In order to do something serious on Node.js, you need to install additional modules, for example, express (a framework for web applications). However, conceptually PHP is simpler.
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
Bruno: said nothing
James: I agree. Node.js is a younger technology, so now it’s worse with support and documentation. This problem, however, becomes less and less serious as Node matures.
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.
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
Bruno: I strongly disagree with this point. Although PHP has its own characteristics, many of them have been fixed recently, and many oddities are removed in the newest versions. On the other hand, the same is in the JS world. With regards to the server part, then I also do not agree. The client side and the server side are different things, and you, in any case, will have to switch your brain, the same syntax on the server if we write to Node.js if we write JavaScript code for the browser. Having worked with JS and PHP, in due time, I like the first less and less, although these are purely personal preferences.
James: I love javascript. I know, it has its quirks, and I know that there are several pitfalls, but ECMAScript 2015 fixes a lot while adding interesting new features to the language. JavaScript is both powerful and flexible, and can accommodate many different programming styles. Unlike PHP, I love writing in JavaScript. Node.js wins this round.
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
Bruno: While the composer was originally inspired by npm, now it's even better than npm. Composer will not damage your system if you want to install two versions of the same library, unlike npm. Also, unlike npm, composer can set recursive dependencies, while npm just can't do it. Npm also has completely terrible error messages, which are called "friendly." Finally, npm doesn’t really work with Vagrant, and thus prevents you from getting started properly, not to mention the fact that they don’t pay attention to the desires of their users. There was an error that was open for many years, because of which it was not suitable for users on Windows - this is hardly a small user base. PHP has its share of errors, of course, also stupid, but they have not repelled whole OS for many years.
James: I love npm. It's easy to use, and there are thousands of packages available for almost any need. I also like the fact that npm allows you to choose between global and local installation of packages (as opposed to languages like Ruby, where this is the standard for your gems). Tools such as bower and grunt have a permanent place in my work, and have increased my productivity many times over.
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
Bruno: Well, firstly, we compare PHP with Node.js, and not PHP with JS. Secondly, we compare languages and environments where they can work. To say that a monkey is better than a fish, because the fish could not climb a tree is just silly. But both monkeys and fish can swim, so let's compare how well they do it.
James: Several features that make Node.js so popular (speed, scalability, compatibility with JSON, low resource utilization) allow it to be used for many other uses, for example, development microcontrollers (IoT). I mean, who doesn't love robots?
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
Bruno: I'll draw here. PHP has an age advantage, giving it more options, but even here it was possible to suffer from some obsolete integrated things, for example, the MySQL extension, which we finally managed to get rid of in PHP7.
James: Not sure that I agree with that. I would hope by the example of “old, less popular things in technology”. One of the main advantages of Node.js is that it understands JSON. JSON is probably the most important format for exchanging data on the Internet, or interacting with NoSQL databases. When you work with Node.js, data can flow neatly in layers without the need for reformatting. You have one syntax when communicating with the database.
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
Bruno: silent
James: In the future, Node.js will catch up. Typically, the PHP web server is distributed with MySQL. To see some kind of php code, all you need to do is create a file with the .php extension and shove the code between <? Php and?>, Upload the file to the server, enter the path to this file in the address bar. The same cannot be said about Node.js. There are, of course, many hosting options for Node.js, but they invariably require more detailed configuration and access to the command line, which potentially repels beginners.
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
Bruno: Misconceptions abound. First, performance discussions are controversial. Any performance gains depend solely on the experience of the developers and the type of application. But even if this message is not convincing, here are some of my own arguments: PHP works quite well with a multi-threaded embedded web server, this allows you not to use the external server completely, but this is not recommended (for now). There are also super fast servers, such as Nginx, which make the whole process of running PHP and delegating requests to it imperceptible. Projects such as HHVM and Appserver add powerful asynchronous and multi-threaded aspects, PHP7 itself will be much more powerful than the previous version update. Yes, the php application receives only static, and lives only once per request, however, this can be bypassed: memcached, ajax. JS server applications run in one request by default. PHP needs to constantly make requests to the page in order to update it, but more than that - the life of a single request is only an advantage, because we make a request again for PHP, our application is restored (every time you request) when it is used, thus we we avoid problems with memory, cleaning of garbage.
James: Node.js is distributed as a platform with high performance, low latency applications. Thanks to non-blocking I / O mechanism and Google Chrome V8 technology, Node.js has become synonymous with the words “fast” and “scalable”. There are numerous stories about how Node.js brought significant performance gains to the company and produced an increase in developer productivity. I am pleased with this, but again, this is a controversial point.
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
Bruno: You certainly look in the wrong places. The PHP community is incredibly passionate and very active. There are more than 20 major conferences a year, and some amazing topics get their discussions. The emergence of HHVM and PHP7 also makes itself felt. In addition, I would like to say that it is interesting when you see that the Node.js developers still did not learn how to change the version numbers (version v0.12.5 at the time of this writing), even after 6 years of development. Many immaturities, combined with critical but ignored old bugs, can repel entire operating systems, all of which is something that a serious developer will think twice. I do not like Node.js. Moz hostility is based on negative experiences mainly when working with npm. It is possible to change everything in the future, but now, I am full of fear and despair when I have to use Node.js again. , , , . , . . — , , , . , , , , .
: Node.js . Node.js-. , , , 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
Bruno: silence .
James: The draw was a fair result of this round. Node.js is a rising star, but massive PHP won't overthrow it. In conclusion, you can add, if your tool seems to be a hammer, then every problem seems to be a nail. Node.js is not ideal for every script, and in fact it is better to think many times when and where it makes sense to use it. However, where Node.js is bad, it’s even very good. You can make an informed choice and choose the best tool for your work.
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!