Home > Backend Development > PHP Tutorial > PHP vs Nodejs in-depth discussion

PHP vs Nodejs in-depth discussion

WBOY
Release: 2016-08-08 09:20:54
Original
894 people have browsed it

The Internet 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 ​​that focus 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 do 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 now 80% of the world's servers are running PHP. 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. Node.js is new but has quickly gained huge 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, and the main reasons are as follows:

  1. First of all, they are worthy of comparison. Both are open source, both are dedicated to web development, and both can be used for similar projects.
  2. PHP has been released for a long time, but Node.js is just emerging and receiving more and more attention. Should PHP programmers believe the propaganda of Node.js? Should you consider switching languages?
  3. 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 someone 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. . . . . .

Round 1: Getting Started

How fast is it to create a “Hello World” web page? In PHP:

<?php
    echo 'Hello World!';
?>
Copy after login

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, that’s not all. This code will only run on a web server with PHP installed (PHP has a built-in server, although 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');
Copy after login

在浏览器中访问 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 胜。理由如下:

  1. JavaScript 是世界上最难理解的语言 — 但是,当哪天你顿悟以后,概念一通,就会发现其他语言都太过笨拙了。
  2. JavaScript 代码比 PHP 简洁。例如,你再不需要跟 JSON 转来转去—— UTF-8 也不用
  3. 全栈工程师可以同时在客户端与服务端使用 JavaScript 。大脑不需要来回切换。
  4. 深入理解 JavaScript 会让你更想用它,但是 PHP 不是这样。

第四轮:开发工具

两种技术都有一些很好的编辑器,集成开发环境,调试器,验证器和其他工具。我认为这是平局,但是这里有一些工具给 Node.js 些许优势:NPM-包管理器。 NPM 允许你安装和管理依赖,设置配置变量,定义脚本和其他。

PHP 的 Composer 项目受 NPM 激发,在有些方面更强。但是,PHP 在默认情况想不提供,活动库较小,在社区的影响更小。

Grunt 和 Gulp 之类革新了开发方法的构建工具的壮大,NPM 也有一部分功劳。有时候 PHP 开发者也许想要/需要安装 node.js,这不是倒退。

第五轮:环境

技术可以在哪使用和部署?支持哪些平台和生态系统?网页开发者经常需要开发一些并不完全针对网页的应用,比如构建工具,迁移工具,数据库转换脚本等。

PHP 有办法开发桌面应用和命令行工具,但是你不会使用他们。本质上,PHP 是一个服务端技术,他很擅长该领域,但是很少延伸到这之外。

若干年前,JavaScript 被认为限制很多,有一些边缘技术,但是他的主战场还是浏览器。Node.js 已经改变了这一感觉并井喷出了很多 JavaScript 项目,你可以在任何地方使用 JavaScript:浏览器,服务器,终端,桌面甚至嵌入式系统,这使得 JavaScript 无处不在。

第六轮:集成

开发技术很受限制,除非他们能与数据库和驱动集成。PHP 在这方面很强,PHP 面世已经很多年,他的扩展使他能和拥有主流或冷门的 API 的服务器直接通讯。

Node.js 正在迎头直追,但是你也许会为某些老旧,冷门的技术寻找成熟集成组件而头疼。

第七轮:托管和部署

部署你绚丽新应用到在线网页服务器有多容易?这是 PHP 的另一次完胜。随机联系某个网页托管公司你可以找到对主要的 PHP 支持,也许还免费附送 MySQL。对于沙盒,PHP 被认为更简单,有风险的扩展可以被禁用。

Node.js 是个不同的野兽,服务端的应用永远运行。你需要一台物理/虚拟/云或定制的服务器环境,最好有 root 权限,这对有些服务器来说遥不可及,特别是那些共享的服务器,你有可能让整台服务器宕机。

Node.js 托管将会变得简单,但是我认为他永远没法像 FTP 上传一些 PHP 文件那么方便。

第八轮:性能

PHP 很勤快,有很多项目跟选项可以使它跑得更快。即使那些对性能要求很严苛的 PHP 开发者也几乎不会担心速度问题, 但是 Node.js 性能通常更好一些。 当然,性能很大程度上决定于开发团队的经验以及是否上心, 但是 Node.js 还是有如下几条优势的:

更少的依赖

所有对 PHP 应用的请求都必须经过一个 WEB 服务器的路由,来启动 PHP 的解释器运行 PHP 代码。Node.js 不需要这些依赖, 而且你基本一定会使用一个带服务器的框架,像 Express,他很轻量,很好的扮演你应用的一部分。

更小更快的解释器

Node.js 的解释器比 PHP 的更小更灵活。 他并不受旧版语言遗留兼容问题的拖累,而且 Google 在 V8 引擎性能改善上出了大力。

应用永久在线

PHP 遵循标准客户端-服务端模型。 每个页面请求都会初始化应用; 你读取配置参数、连接数据库、读取信息、渲染 HTML。Node.js 应用持久运行,只需要启动一次。例如,你可以创建一个单独数据连接对象,然后所求请求一起复用。公认的,PHP 也有一些途径来实现,比如使用 Memcached ,但是这已经不是语言的标准特性了。

事件驱动,无阻塞 I/O

PHP 跟其他多数服务端语言采用阻塞执行的模型。 当你执行一个命令,比如从数据库取数据,那么必须等这个指令执行完成后,才会执行下面的内容。 Node.js 通常不会等的。 取而代之的是, 你需要提供一个回调函数,这个函数当指令执行完后会被调用一次。例如:

// fetch records from a NoSQL database
DB.collection('test').find({}).toArray(process);
console.log('finished');

// process database information
function process(err, recs) {
    if (!err) {
        console.log(recs.length + ' records returned');
    }
}
Copy after login

这个例子中, 控制台会先输出‘finished’,然后输出‘N records returned’,因为 process 函数是所有数据返回的时候才被调用的。 换句话说,当解释器在其它进程处理的时候可以干些别的事情。

注意情况比较复杂,还有几个警告:

  • Node.js/JavaScript 只能在单线程上运行,但是大多数 web 服务器都是多线程,而且并发的处理请求。
  • 一个用户长时间运行的 JavaScript 处理会阻止其它用户的代码执行,除非拆分任务或者使用Web Workers。
  • 基准测试是主观的和有缺陷的;可以找到一些例子 Node.js 比较好,而一些相对的例子 PHP 比较好 。程序员只是在尝试证明他们的信仰!
  • 书写异步的事件驱动的代码非常复杂,非常有挑战性。

我只能从我的经验来讲: 我的 Node.js 应用要明显比 PHP 的同等应用要快。你的可能不是,但是不试是永远不会知道的。

第九轮:开发者激情

这会超出”常见网页开发挑战“这样的目标,但是这很重要。如果你恐惧每天写代码,那你无所谓哪门语言更好。

很难为此做出比较但是一些 PHP 开发者对 PHP 这门语言很有激情。你最近一次读到让你走心的 PHP 文章或幻灯片是什么时候?也许已无需再说?可能是更低的曝光度?或者我没找对地方?PHP7 有一些新的功能,但是该技术已经原地踏步很多年了,虽说如此,很少有开发人员对PHP发牢骚。

JavaScript 分离了社区,有人爱也有人恨,一些程序员在中间犹豫不决,经管如此,对 Node.js 的反馈大多积极,她正处于风口浪尖,一部分原因是因为她很新,赞誉不一定持续。目前,Node.js 赢得本轮。

第十轮:前景

您选择采用哪种服务端语言并不重要;即使她不再被更新也会照样继续工作(yay ColdFusion!)尽管使用量上趋于稳定但是很多人依然使用 PHP,我打包票她还能再坚挺二十年。

Node.js 崛起得很迅速,她提供了一种现代的开发方式,使用和客户端开发一样的语法同时支持 HTML5 变革式的特征,比如网络套接字和服务端发送事件。尽管大家对该语言的分叉函数有些争议,但是Node.js的使用量还是呈指数级增长。

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.

Final winner

Final score: Node.js won 5 rounds, PHP won 4 rounds, one round was tied. 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 bit of a learning curve and is not ideal for newbies but she wins this showdown. 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 has introduced an in-depth discussion of PHP vs Nodejs, including all aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template