Home > Backend Development > PHP Tutorial > PHP vs Node.js, phpvsnode.js_PHP Tutorial

PHP vs Node.js, phpvsnode.js_PHP Tutorial

WBOY
Release: 2016-07-13 09:46:24
Original
992 people have browsed it

PHP vs Node.js, phpvsnode.js

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 ​​​​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 servers around the world. 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:

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!';
?>
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, 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. Usually 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

Before accessing http://127.0.0.1:3000/ in the browser, you need to enter node hello.js in the terminal to start the application. With the five lines of code above, we have created a small web server that, although surprising, is difficult to understand even for people with strong client-side JavaScript experience.

PHP wins this round as it is conceptually simpler. Someone with a little knowledge of PHP declarations can develop something useful. PHP has more software dependencies, but PHP concepts are less cumbersome for newbies.

Knowing some JavaScript and developing Node.js applications are two different things. The Node.js development method is different from most server-side technologies. You need to understand some quite complex concepts first, such as shutdown and callback functions.

Round 2: Help and Support

Without the help of official documentation and resources (courses, forums, stack overflow) you will be in trouble. PHP wins this round easily, she has tons of guides and two decades of Q&A. No matter what you want to do, there will always be someone who has faced the same problem before you.

Node.js has good documentation, but is younger and offers less help than PHP. JavaScript has been on the market as long as PHP, but the main help has been developed for browsers, which is basically not helpful.

Round 3: Language Grammar

Are the declarations and structures logical, simple and easy to use?

Unlike some languages ​​and frameworks, PHP does not restrict you from writing in a specific way, it is up to you to do whatever you want. You can start with a few lines of program, then add some methods, then write some simple PHP4 pattern objects, and finally write an elegant object-oriented MVC pattern PHP5 application. Your code may be confusing at first, but it will work, and it will get better as your understanding deepens.

PHP’s syntax may be slightly adjusted between versions, but backward compatibility is generally done well. But unfortunately, this also leads to a problem: PHP is confusing. For example, how to count the number of characters in a string? Is it count? str_len? or strlen? Or maybe mb_strlen?PHP has hundreds of functions, and the naming rules are not entirely consistent. You can try writing a few lines of code without checking the documentation.

JavaScript is relatively simple, with only dozens of core statements. However, the syntax is often criticized by developers because its prototype object model looks approachable, but in fact it is not. And various mathematical errors (0.1 0.2 ! = 0.3) and type conversion confusion ('4' 2 == '42' and '4' - 2 == 2) also caused a lot of complaints, but these situations rarely lead to What's the problem? Most languages ​​have this excuse.

PHP has its advantages, but I judge Node.js to win this round. The reasons are as follows:

Round 4: Development Tools

Both technologies have some great editors, integrated development environments, debuggers, validators and other tools. I think it's a tie, but here are some tools that give Node.js a slight edge: NPM - Package Manager. NPM allows you to install and manage dependencies, set configuration variables, define scripts and more.

PHP’s Composer project is inspired by NPM and is stronger in some aspects. However, PHP is not provided by default, has a smaller active library, and has less impact on the community.

NPM is also partly responsible for the growth of build tools such as Grunt and Gulp that have revolutionized development methods. Sometimes PHP developers may want/need to install node.js, this is not a step backwards.

Round 5: Environment

Where can the technology be used and deployed? What platforms and ecosystems are supported? Web developers often need to develop applications that are not entirely targeted at web pages, such as build tools, migration tools, database conversion scripts, etc.

PHP has ways to develop desktop applications and command line tools, but you won’t use them. At its core, PHP is a server-side technology that is good at that but rarely extends beyond that.

A few years ago, JavaScript was considered to be very restrictive and had some edge technologies, but its main battlefield was still the browser. Node.js has changed that feeling and spawned a lot of JavaScript projects. You can use JavaScript everywhere: browsers, servers, terminals, desktops and even embedded systems, making JavaScript ubiquitous.

Round Six: Integration

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.

  第八轮:性能

  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 势必会蚕食 PHP 的市场份额,但是我不认为她能完全取而代之。两种技术都有光辉的未来。我宣布本轮平手。

  最终赢家

  最终分数:Node.js 赢得5轮,PHP 赢得4轮,一轮平手。原以为会倒向其中一方,结果比我预想的更中庸一些。

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.

Original address: http://www.sitepoint.com/sitepoint-smackdown-php-vs-node-js/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1033243.htmlTechArticlePHP vs Node.js, phpvsnode.js The network is in an era of rapid development. Server-side developers are very confused when choosing a language. There are long-term dominant languages...
Related labels:
php
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