Simple Talk about PHP vs node.js_php tips

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

The network is in a rapid development era. Server-side developers are confused when it comes to choosing a language, with 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 working well, your choices seem less important.

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

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

Php

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

The PHP code can be mixed with HTML. It doesn't take much practice for beginners to quickly write valuable code. This makes 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 using 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 library of functions to handle network requests and responses, so you don't need a separate server (Apache, ngix) or other dependencies.

Node.js, though very new, soon gained great popularity. In many large companies are in use, such as: Microsoft,yahoo,linkedin and PayPal.

Our favorite C #, Java, Ruby, Python, Perl, Erlang, C + +, go, Dart, Scala, Haskell, and so on, what about them?
If the article is compared to all the various parameters of the language, the article will be very long, you will still read it? Do you expect a programmer to understand all of the program development languages? This is clearly not possible. I mainly compared PHP and Node.js, the main reasons are as follows:

First, they are worth comparing. Both are open source, both dedicated to web development and can be used for similar projects.

PHP has been published for a long time, but Node.js is just emerging and getting more and more attention. are PHP programmers supposed to believe in node.js propaganda? Should you consider translating languages?

I know, love programming languages, since the 1990s I have been using PHP and JavaScript, and also have a few years of node.js experience. I also dabble in other techniques, but I can't make an objective assessment of them here.

In addition, it is not important to say how many languages, because there will always be certain places where some people complain that I did not mention their language.

SitePoint on the race

Programmers spend a lot of time promoting 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 a number of factors. Subjectively, you will be pushing and defending your technical decisions.

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

Evaluation methods

The following 10 rounds are compared to PHP and Node.js. Each round will consider common development challenges that can be applied to any web technology. We don't go into the details too much; few people care about the value of random number generators or array sorting.

Winning the most rounds will be the winners. You ready? Let's start the battle.

First round: Start

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. 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 Web server that has PHP installed (PHP has a built-in server, though it's 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 a setup program that is built in advance, such as XAMPP or virtual machine mirroring (such as vagrant). A simpler way: Upload your files to any web host.

In contrast, it is easy to install node.js. 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 you can access http://127.0.0.1:3000/in the browser, you need to start the application in the terminal input node hello.js. With the five-element code above, we've created a small Web server, which is surprisingly impressive, but it's hard to understand even those with strong client JavaScript experience.

PHP is simpler in concept, so it wins the round. People who know a little bit about PHP statements can develop something useful. PHP has more software dependencies, but the concept of PHP is less cumbersome for beginners.

Understanding some JavaScript and developing node.js applications are two different things, node.js development methods and most server-side technologies, you need to figure out some fairly complex concepts, such as shutdown 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 is easy to win in this round, she has a lot of guidelines and 20 years of Q&a. No matter what you want to do, there will always be someone who has already 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 for as long as PHP, but the main help is for browsers, and that's basically not helping.

The third round: language grammar

Is the statement and structure logical and easy to use?

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

PHP syntax may be slightly tweaked between versions, but backwards compatibility is generally good. 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? or Mb_strlen? PHP has hundreds of functions, and naming rules are not exactly the same. You can try writing a few lines of code without looking at the document.

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

PHP has his advantages, but I node.js win this round. The reasons are as follows:

JavaScript is the most difficult language to understand in the world-but when you have an epiphany, you will find that other languages are too clumsy.

JavaScript code is simpler than PHP. For example, you don't need to--utf-8 with JSON anymore.

All-Stack engineers can use JavaScript at both the client and server side. The brain doesn't need to switch back and forth.

A deep understanding of JavaScript will make you want to use it more, but not PHP.

Fourth round: Development tools

Both technologies have 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 some advantages: npm-Package Manager. NPM allows you to install and manage dependencies, set configuration variables, define scripts, and more.

The PHP Composer project is inspired by NPM and is stronger in some ways. However, PHP is not available by default, the activity library is small, and the impact on the community is smaller.

NPM has also contributed to the growth of the building tools that 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 the 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 building tools, migration tools, database conversion scripts, and so on.

PHP has a way to develop desktop applications and command-line tools, but you won't use them. Essentially, PHP is a server-side technology, and he's good at it, but it's rarely extended.

A few years ago, JavaScript was thought to be a lot more restrictive, with some edge technology, but his main battlefield was browser. Node.js has changed the feeling and blowout out 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: Integrated

Development technology is very limited unless they can integrate with databases and drives. PHP is very strong in this field, PHP has been available for many years, and his expansion enables him to communicate directly with servers that have mainstream or unpopular APIs.

Node.js is chasing straight ahead, but you may have a headache for some old, unpopular technology looking for sophisticated integration components.

Seventh round: Managed and deployed

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

Node.js is a different beast, and service-side applications run forever. You need a physical/virtual/cloud or custom server environment, preferably with root privileges, which are remote to some servers, especially those shared servers, and you may be able to disable the entire server.

Node.js hosting will be easy, but I don't think he'll ever be able to upload some PHP files like FTP.

Eighth round: Performance

PHP is very diligent, with many items and options to make it run faster. Even those PHP developers who are very demanding on performance are less likely to worry about speed, but node.js performance is usually better. Of course, performance is largely determined by the experience of the development team and whether or not to focus on it, but Node.js has the following advantages:

Less Reliance

All requests for PHP applications must be routed through a WEB server to launch the PHP interpreter to run the PHP code. Node.js do not need these dependencies, and you will basically use a server-like framework, such as Express, he is very lightweight, very good to play a part of your application.

Smaller, faster interpreters

Node.js's interpreter is smaller and more flexible than PHP. He is not burdened by legacy compatibility issues, and Google has made a strong V8 engine performance improvement.

Apply Permanent Online

PHP follows the standard client-server model. Each page request initializes an application; You read configuration parameters, connect databases, read information, Render HTML. The Node.js application is persistent and only needs to be started once. For example, you can create a separate data connection object, and then ask for the request to be reused together. Admittedly, PHP has some ways to implement it, such as using Memcached, but this is no longer a standard feature of language.

Event driven, no blocking I/O
PHP uses a blocking execution model with most other service-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 usually don't wait. Instead, you need to provide a callback function that is called once after the instruction has been executed. For example:

In this example, the console first outputs ' finished ' and then outputs ' N records returned ' because the process function is invoked when all data is returned. In other words, the interpreter can do something else while the other process is processing it.

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

Node.js/javascript can only run on a single thread, but most Web servers are multi-threaded and concurrent processing requests.

A user's long-running JavaScript processing prevents other users from executing the code unless the task is split or the web workers is used.

Benchmark tests are subjective and flawed; some examples can be found node.js better, while 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 application. You may not, but you will never know if you don't try.

Nineth Round: Developer Passion

This goes beyond the "common Web development Challenge," but it's important. If you're afraid to write code every day, you don't care which language is better.

It's hard to make comparisons but some PHP developers are passionate about the language of PHP. When was the last time you read a PHP article or slide that took you to the heart? Perhaps there is no need to say? 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 years, and even so, few developers have complained about PHP.

JavaScript separates the community, some people love others hate, some programmers hesitate in the middle, management so, node.js feedback is mostly positive, she is at the cusp, partly because she is very new, praise does not necessarily last. At present, Node.js won the round.

Tenth round: the future

It is not important that you choose which server language to use, even if she is no longer updated she will continue to work (Yay coldfusion! Although the usage is stable but a lot of people still use PHP, I'm packing her up for another 20 years.

Node.js has risen rapidly, providing a modern way of developing, using the same syntax as client-side development to support HTML5 transformative features such as network sockets and server-side sending events. Although there is some controversy about the bifurcation function of the language, the usage of node.js is exponential growth.

Node.js is bound to erode PHP's market share, but I don't think she can replace it entirely. Both of these technologies have a glorious future. I declare this round of the tie.

Final winner

Final score: Node.js won 5 rounds, PHP won 4 rounds, a round of deuce. I thought I would fall to one of them and the result was a little more moderate than I expected.

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

But do not belittle php,php still have vitality, you should not because node.js faster, new or trendy some to follow node.js trend. PHP is easy to learn and still supports professional programming skills, helping to be ubiquitous and easy to develop. Even die-hard node.js developers have had to consider using PHP in simple Web sites and applications.

My advice: Evaluate options and choose a language based on your needs, which is far more reliable than the "comparison" article.

The above is the entire contents of this article, I hope you can enjoy.

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.