The network is in an era of rapid development. Server-side developers are very confused when it comes to choosing a language. There are long-dominant languages such as C, Java, and Perl, as well as languages focused on web development, such as Ruby, Clojure, and Go. As long as your project is running well, your choice won't seem that important.
But how to let these new web developers make the right choice?
I don’t want to start a war between the two camps of PHP and NodeJs. I will compare the development status of these two languages:
PHP
Rasmus Lerdorf created PHP in 1994. It is run by components installed on the web server (Apache, Ngix).
PHP code can be mixed with HTML. Beginners can quickly write valuable code without much practice. This has made PHP become more and more popular, and PHP is now running on 80% of the world's servers. WordPress, a content management system used by a quarter of the world’s websites, is written in PHP.
Node.js
Ryan Dahl created Node.js in 2009. It is based on Google's V8 JavaScript interpretation engine (which is responsible for executing client-side JavaScript code in the Chrome browser). Unlike other languages, Node.js has built-in function libraries for handling network requests and responses, so you don't need a separate server (Apache, Ngix) or other dependencies.
Although Node.js is new, it has quickly gained great popularity. It is used by many large companies, such as Microsoft, Yahoo, LinkedIn and PayPal.
Our beloved C#, Java, Ruby, Python, Perl, Erlang, C, Go, Dart, Scala, Haskell, etc., what about them?
If the article compared the various parameters of all the languages mentioned above, the article would be very long. Would you still read it? Do you expect a programmer to know all programming languages? This is obviously impossible. I mainly compared PHP and Node.js. The main reasons are as follows:
First of all, they are worth comparing. Both are open source, both are dedicated to web development, and both can be used for similar projects.
PHP has been released for a long time, but Node.js has just emerged and received more and more attention. Should PHP programmers believe the propaganda of Node.js? Should you consider switching languages?
I know and love programming languages, I have been using PHP and JavaScript since the 1990s and have a few years of experience with Node.js. In addition, I have also dabbled in other technologies, but I cannot make an objective evaluation of them here.
Also, it doesn’t matter how many languages I compare, because there will always be people somewhere complaining that I didn’t mention their language.
Competition on SitePoint
Programmers spend a lot of time improving their own programming skills. Some people have the ability to extend between programming languages, but those who reach higher levels make their own choices based on many factors. On the subjective side, you will advance and defend your technical decisions.
SitePoint Smackdowns does not take a "choose what's right for you, my friend" perspective. I will make recommendations based on personal experience, requirements and preferences. You may not agree with everything I say, and that doesn't matter. What matters is that your opinion will help others make more informed choices.
Evaluation method
The following will be a ten-round comparison between PHP and Node.js. Each round considers common development challenges that can apply to any web technology. We won't go into too much detail; few people care about the value of random number generators or array sorting.
The one who wins the most rounds will be the winner. Are you ready? Let the battle begin. . . . . .
First round: Starting
How fast is it to create a “Hello World” web page? In PHP:
<?php echo 'Hello World!'; ?>
This code can be placed in any file that can be parsed by the PHP engine - usually, a file with a .php suffix. Just enter the URL in your browser to jump to the file.
Admittedly, this is not all. This code will only run on a web server with PHP installed (PHP has a built-in server, though it's better to use a more robust server). Most operating systems provide server software, such as IIS on Windows, Apache on Mac and Linux, although they require startup and configuration. Typically using a pre-built installer like XAMPP or a virtual machine image like Vagrant. An easier way: upload your files to any web host.
In comparison, installing Node.js is a piece of cake. You can download the installer or use a package manager. Next let’s create the web page in hello.js:
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World!'); }).listen(3000, '127.0.0.1');
在浏览器中访问 http://127.0.0.1:3000/ 之前,你需要在终端输入 node hello.js 来启动应用程序 。通过上面的五行代码,我们已经创建了一个小型的 web 服务器,尽管这很令人吃惊,但是即便拥有很强客户端 JavaScript 经验的人也很难理解。
PHP 在概念上更简单所以赢得本轮。稍微懂得一些 PHP 声明的人就可以开发一些有用的东西。PHP 有更多的软件依赖,但是 PHP 的概念对于新手来说不那么繁琐。
懂一些 JavaScript 和开发 Node.js 应用是两回事儿,Node.js 开发方法和大多数服务端技术不同,你需要先弄明白一些相当复杂的概念,比如关闭和回调函数。
第二轮:帮助和支持
没有官方文档和资源(课程,论坛,堆栈溢出)的帮助你必将举步维艰。 PHP 在本轮轻易胜出,她有大量的指南和二十年的 Q&A。无论你想做什么,总会有人在你之前已经面对过同样的问题。
Node.js 拥有很好的文档,但是更加年轻,能提供的帮助较 PHP 也少。JavaScript 在市面上的时间和 PHP 一样久,但是主要的帮助都是针对浏览器开发的,那基本没啥帮助。
第三回合: 语言语法
声明与结构是不是符合逻辑而且简单好用?
不像一些语言跟框架,PHP 不会限制你按特定的方式编写,具体怎么搞随你。你可以从几行的程序开始,然后添加些方法,进而写一些简单的 PHP4 模式的对象,最后编写优雅的面向对象的 MVC 模式 PHP5+ 的应用。你的代码开始可能比较混乱,但也能工作,而且会随着理解的深入越写越好。
PHP 的语法在版本间可能略有调整,但是向后兼容一般都做得很好。但不幸的是,这也导致了一个问题:PHP 很混乱。例如,怎么统计一个字符串中字符的个数?是 count?str_len? 还是 strlen?亦或 mb_strlen?PHP 有数以百记的函数,而且命名规则也也不完全一致。可以试试不查文档写几行代码。
JavaScript 相对就简单些,只有几十个核心声明。不过语法就经常被开发者喷了,因为它的原型化对象模型看起来平易近人,实际上却不是。而且各种数学错误(0.1+0.2 != 0.3)以及类型转换的混乱('4' + 2 == '42' 和 '4' - 2 == 2)也招致不少抱怨,但这些情况世界很少导致什么问题,多数语言都有这种借口。
PHP 有他的优点,但是这回合我判 Node.js 胜。理由如下:
JavaScript 是世界上最难理解的语言 — 但是,当哪天你顿悟以后,概念一通,就会发现其他语言都太过笨拙了。
JavaScript 代码比 PHP 简洁。例如,你再不需要跟 JSON 转来转去—— UTF-8 也不用
全栈工程师可以同时在客户端与服务端使用 JavaScript 。大脑不需要来回切换。
深入理解 JavaScript 会让你更想用它,但是 PHP 不是这样。
第四轮:开发工具
两种技术都有一些很好的编辑器,集成开发环境,调试器,验证器和其他工具。我认为这是平局,但是这里有一些工具给 Node.js 些许优势:NPM-包管理器。 NPM 允许你安装和管理依赖,设置配置变量,定义脚本和其他。
PHP 的 Composer 项目受 NPM 激发,在有些方面更强。但是,PHP 在默认情况想不提供,活动库较小,在社区的影响更小。
Grunt 和 Gulp 之类革新了开发方法的构建工具的壮大,NPM 也有一部分功劳。有时候 PHP 开发者也许想要/需要安装 node.js,这不是倒退。
第五轮:环境
技术可以在哪使用和部署?支持哪些平台和生态系统?网页开发者经常需要开发一些并不完全针对网页的应用,比如构建工具,迁移工具,数据库转换脚本等。
PHP 有办法开发桌面应用和命令行工具,但是你不会使用他们。本质上,PHP 是一个服务端技术,他很擅长该领域,但是很少延伸到这之外。
若干年前,JavaScript 被认为限制很多,有一些边缘技术,但是他的主战场还是浏览器。Node.js 已经改变了这一感觉并井喷出了很多 JavaScript 项目,你可以在任何地方使用 JavaScript:浏览器,服务器,终端,桌面甚至嵌入式系统,这使得 JavaScript 无处不在。
第六轮:集成
Development techniques are very limited unless they can integrate with databases and drivers. PHP is strong in this regard. PHP has been around for many years, and its extensions allow it to communicate directly with servers with mainstream or less popular APIs.
Node.js is catching up, but you may have a headache looking for mature integrated components for some old, unpopular technologies.
Round 7: Hosting and Deployment
How easy is it to deploy your gorgeous new application to an online web server? This is another big win for PHP. Contact a random web hosting company and you can find support for primarily PHP, and maybe MySQL included for free. For sandboxing, PHP is considered simpler and risky extensions can be disabled.
Node.js is a different beast, server-side applications run forever. You need a physical/virtual/cloud or custom server environment, preferably with root access, which is out of reach for some servers, especially those that are shared, and you risk bringing the entire server down.
Node.js hosting will become easier, but I don’t think it will ever be as convenient as FTP to upload some PHP files.
Round 8: Performance
PHP is very hard-working and has many projects and options to make it run faster. Even performance-critical PHP developers rarely worry about speed, but Node.js performance is generally better. Of course, performance largely depends on the experience and dedication of the development team, but Node.js still has the following advantages:
Less dependencies
All requests to PHP applications must be routed through a WEB server to start the PHP interpreter and run PHP code. Node.js does not require these dependencies, and you will basically use a framework with a server, like Express, which is very lightweight and plays a good part of your application.
Smaller and faster interpreter
Node.js’s interpreter is smaller and more flexible than PHP’s. It is not hampered by legacy compatibility issues with older versions of the language, and Google has made significant efforts to improve the performance of the V8 engine.
The application is always online
PHP follows the standard client-server model. Each page request initializes the application; you read configuration parameters, connect to the database, read information, and render HTML. Node.js applications run persistently and only need to be started once. For example, you can create a single data connection object and then reuse it for all requests. It is recognized that PHP also has some ways to implement it, such as using Memcached, but this is no longer a standard feature of the language.
Event-driven, non-blocking I/O
PHP, like most other server-side languages, uses a blocking execution model. When you execute a command, such as fetching data from a database, you must wait for the command to be executed before the following content is executed. Node.js usually doesn't wait. Instead, you need to provide a callback function that will be called once when the instruction is executed. For example:
In this example, the console will first output ‘finished’, and then ‘N records returned’, because the process function is called when all data is returned. In other words, the interpreter can do other things while other processes are processing.
Please note that the situation is more complicated and there are several warnings:
Node.js/JavaScript can only run on a single thread, but most web servers are multi-threaded and handle requests concurrently.
One user's long-running JavaScript processing will prevent other users' code from executing unless the task is split or Web Workers are used.
Benchmarks are subjective and flawed; you can find some examples where Node.js is better, and some comparative examples where PHP is better. Programmers are just trying to prove their beliefs!
Writing asynchronous event-driven code is very complex and challenging.
I can only speak from my experience: my Node.js application is significantly faster than its PHP equivalent. Yours may not be, but you'll never know if you don't try.
Round 9: Developer Passion
This would go beyond the “common web development challenges” goal, but it’s important. If you fear writing code every day, it doesn't matter which language is better.
It’s hard to make a comparison but some PHP developers are very passionate about the language. When was the last time you read a PHP article or slideshow that moved you? Maybe there’s no need to say more? Maybe lower exposure? Or am I not looking in the right place? PHP7 has some new features, but the technology has been stuck for years, and despite this, few developers are complaining about PHP.
JavaScript has divided the community, some love it and some hate it, and some programmers are hesitant in the middle. Despite this, the feedback on Node. It must last. Currently, Node.js wins this round.
Round 10: Prospects
It doesn’t matter which server language you choose; it will continue to work even if it is no longer updated (yay ColdFusion!). Although usage has plateaued, many people still use PHP, and I guarantee that it will still work. Can stand strong for another twenty years.
Node.js is rising rapidly. It provides a modern development method that uses the same syntax as client-side development while supporting the revolutionary features of HTML5, such as network sockets and server-side sent events. Despite some controversy over the language's forking function, Node.js usage has grown exponentially.
Node.js is bound to eat away at PHP’s market share, but I don’t think it can completely replace it. Both technologies have a bright future. I declare this round a draw.
The final winner
Final score: Node.js won 5 rounds, PHP won 4 rounds, and tied in one round. I thought it would lean towards one of the sides, but it turned out to be more moderate than I expected.
Node.js has a certain learning curve and is not ideal for novices but she won this duel. And, if you’re a solid JavaScript programmer who likes the language, Node.js won’t let you down. It's more up-to-date and provides your own web development experience, so you won't miss PHP.
But don’t belittle PHP, PHP is still vital, and you shouldn’t follow the Node.js trend just because Node.js is faster, newer or trendier. PHP is easy to learn and still supports professional programming skills, help is everywhere and development is simple. Even die-hard Node.js developers have to consider using PHP for simple websites and applications.
My advice is: evaluate the options and choose a language based on your needs. This is much more reliable than a "comparison" article like this one.
The above is the entire content of this article, I hope you all like it.