PHP vs node.js,phpvsnode.js_php Tutorials

Source: Internet
Author: User
Tags rounds small web server upload php hosting install node web hosting

PHP vs Node.js,phpvsnode.js


The network is in a time of rapid development. Server-side developers are confused when choosing a language, with long-term dominance of 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 works well, your choices don't seem so important.

But how do you get these new Web developers to make the right choice?

I don't want to start a war between PHP and Nodejs, and I'll compare the development of the two languages in the field:

    • Php
      Rasmus Lerdorf created PHP in 1994. It is run by a component installed on the Web server (Apache, Ngix).

      PHP code can be mixed with HTML into one piece. It doesn't take much practice for beginners to write very valuable code quickly. This has made PHP more and more popular, and now 80% of the world's servers are running PHP. One-fourth of the world's web sites are in use of a content management system--wordpress, is written in PHP.

    • node. js
      Ryan Dahl created node. JS in 2009. It is based on Google's V8 JavaScript interpretation engine (it is responsible for executing the client's JavaScript code in the Chrome browser). Unlike other languages, node. JS has built-in 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 quickly gets a lot of popularity. In a lot of big companies are in use, for example: Microsoft,yahoo,linkedin and PayPal.

What do we love about C #, Java, Ruby, Python, Perl, Erlang, C + +, Go, Dart, Scala, Haskell, and so on?

If you compare the parameters of all the languages in the article, the article will be very long, will you read it? Do you expect a programmer to understand all the programming languages? This is obviously impossible. I mainly compare PHP and node. JS, the main reasons are as follows:

In addition, it is not important to compare how many languages, because there will always be some people in some places complaining that I did not mention their language.

The race on the SitePoint

Programmers spend a lot of time improving their own programming skills. Some people have the ability to stretch between programming languages, but those who reach higher levels make their own choices based on a number of factors. On the subjective side, you will advance and defend your technical decisions.

SitePoint Smackdowns did not take the "Choose your own, friend" view. I will make recommendations based on personal experience, requirements and preferences. You may not agree with all of the points I've said, and it doesn't matter, it's important that your opinion will give others a more informed choice.

Evaluation method

The following will compare PHP and node. JS in 10 rounds. Each turn will consider common development challenges that can be applied to any web technology. We don't go into the details too much, and few people care about the value of random number generators or array sorting.

Winning the most rounds will be the winner. You ready? Let's start the contest ...

First round: Getting Started

How fast is it to create a "Hello World" Web page? In PHP:

 
  

This code can be placed in any file that can be parsed by the PHP engine-Typically, a file with a. php suffix. Enter the URL in the browser to jump to the file.

There is no denying that this is not all. This code can only run on a PHP-installed Web server (PHP has a built-in server, although it is best to use a more robust server). Most operating systems provide server software, such as Iis,mac on Windows and Apache on Linux, although they need to be started and configured. You typically use an early-build installer, such as XAMPP or virtual machine mirroring (such as Vagrant). A simpler way: Upload your files to any web host.

In contrast, installing node. js is easy. You can download the installer or use the Package Manager. Next, let's create a Web page in Hello.js:

var http = require (' http '); Http.createserver (function (req, res) {    Res.writehead ($, {' Content-type ': ' text/ Plain '});    Res.end (' Hello world! ');}). Listen (3000, ' 127.0.0.1 ');

Before accessing Http://127.0.0.1:3000/in a browser, you need to enter node Hello.js in the terminal to launch the application. With the five lines of code above, we've created a small Web server, although it's surprising, but even people with strong client-side JavaScript experience are hard to understand.

PHP is conceptually easier to win this round. People who know a little bit about PHP statements can develop something useful. PHP has more software dependencies, but the PHP concept is less cumbersome for beginners.

Understanding some JavaScript and developing node. JS applications are two different things, and the node. JS development method differs from most server-side technologies, and you need to understand some fairly complex concepts such as closing and callback functions.

Second round: Help and support

Without the help of official documents and resources (courses, forums, stack overflows) you will be struggling. PHP won easily in this round, she has a lot of guidance and 20 years of Q&a. No matter what you want to do, there will always be people who have faced the same problem before you.

node. JS has good documentation, but is younger and offers less help than PHP. JavaScript is on the market for as long as PHP, but the main help is developed for the browser, which is basically not helpful.

Third round: Language grammar

is the declaration and structure logical and easy to use?

Unlike some languages and frameworks, PHP does not limit you to writing in a specific way, specifically how to do it with you. You can start with a few lines of the program, and then add some methods to write some simple PHP4 schema objects, and finally write an elegant object-oriented MVC pattern php5+ application. Your code may be confusing at first, but it can work, and it will write better as you understand it.

PHP's syntax may be slightly tweaked between versions, but backwards compatibility is generally done well. Unfortunately, this also leads to a problem: PHP is confusing. For example, how do you count the number of characters in a string? is count? Str_len? Or a strlen? Also or Mb_strlen? PHP has hundreds of functions, and the naming conventions are not exactly the same. Try to write a few lines of code without checking the document.

JavaScript is relatively simple, with only dozens of core declarations. However, the syntax is often sprayed by developers, because its prototype object model looks approachable, but it is actually not. and various mathematical errors (0.1+0.2! = 0.3) as well as the confusion of type conversions (' 4 ' + 2 = = ' 42 ' and ' 4 '-2 = = 2) also incur a lot of complaints, but these situations rarely cause problems in the world, and most languages have this excuse.

PHP has his advantages, but this round I sentenced to node. JS wins. The reasons are as follows:

Fourth round: Development tools

Both technologies have some good editors, integrated development environments, debuggers, validators, and other tools. I think it's a draw, but here are some tools to give node. js a bit of an advantage: npm-Package Manager. NPM allows you to install and manage dependencies, set configuration variables, define scripts, and others.

PHP's Composer project is motivated by NPM and is stronger in some ways. However, PHP does not want to provide the default, the activity library is small, the impact in the community is smaller.

NPM has also been part of the process of building tools that have revolutionized development methods such as Grunt and Gulp. Sometimes PHP developers may want/need to install node. js, which is not backwards.

Fifth round: Environment

Where can technology be used and deployed? What platforms and ecosystems are supported? Web developers often need to develop applications that are not entirely web-oriented, such as build tools, migration tools, database conversion scripts, and so on.

PHP has a way to develop desktop apps and command-line tools, but you won't use them. In essence, PHP is a service-side technology that he excels in, but rarely extends beyond.

Some years ago, JavaScript was considered a lot more restrictive, with some edge technology, but his main battlefield was the browser. node. JS has changed this feeling and has produced a lot of JavaScript projects where you can use JavaScript anywhere: browsers, servers, terminals, desktops and even embedded systems, which makes JavaScript ubiquitous.

Sixth round: Integration

Development techniques are limited unless they can be integrated with databases and drivers. PHP is very strong in this area, PHP has been available for many years, his expansion so that he can have mainstream or unpopular API of the server direct communication.

node. JS is chasing straight up, but you may have headaches with some old, unpopular technology looking for mature integration components.

Seventh round: Hosting and deployment

How easy is it to deploy your fancy new app to an online Web server? This is another victory for PHP. Random contact with a web hosting company you can find the main PHP support, and maybe also free MySQL. For sandbox, PHP is considered simpler and risky extensions can be disabled.

node. JS is a different beast, and service-side apps run forever. You need a physical/virtual/cloud or custom server environment, preferably rooted, which is unreachable for some servers, especially those shared servers, and you may be able to get the entire server down.

node. js hosting will be easy, but I don't think he'll ever be able to upload PHP files as easily as FTP.

Eighth round: Performance

PHP is very diligent, there are many projects and options can make it run faster. Even the most demanding PHP developers have little to worry about speed, but node. JS performance is usually better. Of course, performance depends largely on the experience and focus of the development team, but node. JS has the following advantages:

 Less Reliance

All requests for PHP applications must be routed through a WEB server to start PHP's interpreter to run PHP code. node. JS does not need these dependencies, and you will almost certainly use a server-based framework, like Express, which is lightweight and well-played as part of your application.

  Smaller and faster interpreter

node. JS's interpreter is smaller and more flexible than PHP. He is not a drag on legacy compatibility issues, and Google is aggressively improving the performance of the V8 engine.

  Apply Permanent Online

PHP follows the standard client-server model. Each page request initializes the application; You read the configuration parameters, connect to the database, read the information, render the HTML. The node. JS app runs long and needs to be started only once. For example, you can create a separate data connection object and then reuse the request. Admittedly, PHP also has some ways to do it, such as using Memcached, but this is not a standard feature of language.

Event-driven, non-blocking I/O

PHP uses a model of blocking execution with most other server-side languages. When you execute a command, such as fetching data from a database, you must wait for the instruction to complete before executing the following. node. js is usually not waiting. Instead, you need to provide a callback function that will be called once the instruction is executed. For example:

Fetch records from a NoSQL databasedb.collection (' Test '). Find ({}). ToArray (process); Console.log (' finished '); Process database informationfunction process (err, RECs) {    if (!err) {        console.log (recs.length + ' records retur Ned ');}    }

In this example, the console outputs ' finished ' and then outputs ' N records returned ' because the process function is called when all data is returned. In other words, when the interpreter is dealing with other processes, it can do something else.

Note that the situation is more complex and there are several caveats:

    • Node.js/javascript can only be run on a single thread, but most Web servers are multi-threaded and handle requests concurrently.

    • A user's long-running JavaScript processing blocks other users ' code execution, unless they split tasks or use Web Workers.

    • Benchmark tests are subjective and flawed; You can find some examples of node. js better, and some relative examples of PHP are better. Programmers are just trying to prove their faith!

    • Writing asynchronous event-driven code is very complex and challenging.

I can only tell from my experience that my node. js application is significantly faster than PHP's equivalent. You may not be, but you will never know until you try.

Nineth Round: Developer Passion

This is beyond the common web development challenge, but it's important. If you're afraid to write code every day, it doesn't matter which language is better.

It's hard to make a comparison, but some PHP developers are passionate about the language of PHP. When was the last time you read a PHP article or slideshow that let you go? Maybe you don't have to say it again? Could it be a lower exposure? Or did I not find the right place? PHP7 has some new features, but the technology has been in place for many years, and despite this, few developers have complained about PHP.

JavaScript separates the community, some love it, some hate it, some programmers hesitate in the middle, and so, the feedback to node. JS is mostly positive, she's on the cusp, partly because she's new and praise doesn't necessarily last. Currently, node. JS wins this round.

Tenth round: Prospects

It doesn't matter which server language you choose to use, even if she is no longer updated (Yay coldfusion! Despite the steady usage, many people still use PHP, and I'll be able to do it for 20 more years.

node. JS has risen rapidly, providing a modern way to develop the same syntax as client-side development to support HTML5 transformative features such as network sockets and server-side send events. Although there is some controversy about the fork function of the language, the usage of node. JS is growing exponentially.

node. JS is bound to nibble on PHP's market share, but I don't think she can replace it completely. Both technologies have a glorious future. I declare this round a tie.

Final winner

Final score: node. JS wins 5 rounds, and PHP wins 4 rounds, one round tie. I thought it would pour to one side, and the result was a little more moderate than I expected.

node. JS has a certain learning curve and is not ideal for beginners but she has won the duel. And if you're a reliable JavaScript programmer who likes the language, node. js won't disappoint you. She's more trendy and offers her own web development experience, and you won't miss PHP.

But don't belittle the php,php is still alive, you shouldn't be quick with node. js, new or trendy to follow the node. js Trend. PHP is easy to learn and still supports professional programming skills, helping to be ubiquitous and easy to develop. Even the loyal node. JS developers have to consider using PHP for simple sites and applications.

My advice: Evaluate options and choose a language based on your needs, which is much more reliable than the "compare" article in this article.

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

http://www.bkjia.com/PHPjc/1033243.html www.bkjia.com true http://www.bkjia.com/PHPjc/1033243.html techarticle PHP vs Node.js,phpvsnode.js Network is in a rapidly evolving era. Server-side developers are very confused when choosing a language, have a long-term dominant language, ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.