PHP vs Node.js – The battle between two web programming technologies

Tram Ho

Web development technology is always changing. Server-side developers are having an embarrassment of choosing a long-lived, weighted language like Java, C, and Perl for newer people, or web-focused languages ​​like Ruby, Clojure and Go. Which language you choose is rarely important, but it is important that your application can work well.

But how can newcomers enter the field of web development give a correct choice?

I hope I don’t start a jihad, but I will compare the pros and cons of these two technologies together:

PHP was created by Rasmus Lerdorf in 1994. It is handled by an interpreter that is usually installed as a module in a web server like Apache or Nginx.

PHP code can be interleaved with HTML. It’s not the best way to write code, but new people who come into contact with the language can create useful code very quickly. That contributes to the popularity of this language, and PHP is currently being used in more than 80% of web servers in the world. That result is partly due to WordPress – it’s a PHP content management system that uses about a quarter of all websites.

Node.js was created by Ryan Dahl in 2009. It uses Google’s V8 JavaScript engine to create power for client-side code in Chrome web browser. This platform has built-in libraries to handle web requests and responses – you don’t need a separate web server or other dependencies.

Node.js is relatively new but has quickly attracted the attention of the community. It is used by companies like Microsoft, Yahoo, LinkedIn and PayPal.

C #, Java, Ruby, Python, Perl, Erlang, C ++, Go, Dart, Scala, Haskell, etc … where?

An article that goes against all technologies will be very long. Will you take the time to read it? Do you think there is a certain programmer proficient in all of them? Therefore I will limit this comparison to only between PHP and Node.js because:

  1. That is a good comparison. They are all open source, mainly aimed at web development and applied to similar projects.
  2. PHP is a long-established language but Node.js is an emerging technology that is currently receiving much attention. Should PHP programmers believe in hype about Node.js? Should they switch to this new technology?
  3. I know I love these programming languages. I have developed with PHP and JavaScript since the late 1990s, and a few years of Node.js. I have also learned other technologies, but cannot make them fair in this review.

Besides, it doesn’t matter how much I compare languages. Someone, somewhere, will complain that I didn’t put in their favorite language!

About the purpose of the competition

Programmers spend many years honing their skills. Some people have their own “gut” language, but those who have reached the Ninja level often choose technology based on many factors. It is also subjective; You will often be motivated and protected by your technology decisions.

That said, this article is not going to give the advice of “use any technology that is right for you, dear friend” . I will give advice based on my own experiences, needs and biases. You will agree with some points and disagree with other points; that’s the great thing – your opinions will help others make better decisions.

Evaluation methods

PHP and Node.js will be compared in ten rounds. Each round is a common development challenge that can be applied to any web technology. We will not go too deep into details; Very few people will care about the values ​​of random number generator or array sorting algorithms.

The final winner will be the winning technology in the most innings. Are you ready? Let the match begin!

First half: Start

How to build a website that publishes “Hello World” quickly? In PHP:

This code can be placed in any file interpreted by the PHP engine – often with the .php extension. Enter the URL that mapped to that file in your browser and that’s done.

Admittedly, things aren’t that simple. That code will only run through a web server with PHP installed. (PHP has a built-in server, though, but you should use something stronger). Most operating systems provide server software such as IIS on Windows or Apache on Mac and Linux, although they need to be enabled and configured. If you want to be simpler, you can use a pre-built set-up like XAMPP or a virtual OS image (like Vagrant ). Even easier: just upload your files to any web server.

Compared to the above, installing Node.js is very easy. You can download the installer or use a package manager. Then, create the Hello World website in the hello.js file:

You need to start that application from the terminal with the node hello.js command before you can see the results at in your browser. We have created a small web server in 5 lines of code, this is great, but even those with a lot of experience in client-side JavaScript programming will have to work hard to understand it.

PHP has a simpler concept and wins in this half. Those who know some PHP commands can write down something useful. It depends a lot on software, but PHP’s concept is easier to understand for new programmers.

There is a big gap between knowing a little bit about JavaScript and programming Node.js applications. Its development approaches differ from most other server-side technologies, and you need to understand some rather complex concepts such as function closures and callbacks.

Second half: Help and support

You won’t be able to go far without some development support from official documents and resources like courses , forums and StackOverflow. PHP wins this round easily; It has an excellent guide and Q&A questions accumulated during the past 20 years. Whatever you are doing, someone has encountered a similar problem in the past.

Node.js has very good documentation, but it is quite new and has little help. JavaScript is as old as PHP, but most of the help is only related to development in the browser. That doesn’t help much.

Session 3: Language syntax

Are commands and structures reasonable and easy to use?

Unlike some other languages ​​and frameworks, PHP doesn’t require you to work correctly in any direction. You can start with a program with a few lines of code, add functions, write simple code like objects in PHP4 and finally write beautiful applications towards MVC object-oriented versions PHP5 +. Your code can be very messy, but it will work and grow with your understanding.

PHP syntax may vary between versions, but backward compatibility is generally very good. Unfortunately, this leads to a problem: PHP will become a mess. For example, how to count the number of characters in a string? Do you use count ? str_len ? strlen ? mb_strlen ? There are hundreds of functions and they are named inconsistently. Try writing a few lines of code without referring to the tutorial.

JavaScript is relatively more concise, with a few dozen core statements. That said, its syntax attracts developers, because its prototype object model looks familiar but not necessarily so. You will also find complaints about math errors ( 0.1 + 0.2 != 0.3 ) and the type confusion when doing the conversion ( '4' + 2 == '42' and '4' - 2 == 2 ) – but these situations rarely cause problems, and all other languages ​​suffer from similar problems.

PHP has its benefits, but I give the third round victory to Node.js. For the following reasons:

  1. JavaScript is still the most misunderstood language in the world – but once the concepts are clarified, it makes other languages ​​seem cumbersome.
  2. JavaScript code is more concise than PHP. For example, you won’t need to translate to / from JSON and – fortunately – both UTF-8.
  3. Full-stack programmers can use JavaScript on both client and server. Your brain will not need to switch modes.
  4. Understanding JavaScript makes you want to use it more. I cannot say the same for PHP.

Game 4: Development tools

Both technologies have many editors, IDE, debugger, validator and other tools. I’m going to see this as a draw, but there’s a tool that makes Node.js win, it’s: npm – Node Package Manager. npm allows you to install and manage dependencies, set up configuration variables, define scripts and much more.

PHP’s Composer project is influenced by npm and is better in some respects. However, it is not provided with PHP by default, there is a smaller active repository and has little impact in the community.

npm is partly responsible for the development of building tools such as Grunt and Gulp that revolutionized application development. PHP programmers will probably want / need to install Node.js at some point. The opposite is not true.

Game 5: Environment

Where can these technologies be used and deployed? What platforms and ecosystems are supported? Web developers often need to create applications that are not restricted to just that site, such as building tools, conversion tools, database conversion scripts, etc.

There are many ways to use PHP in developing desktop and command-line applications. But you will not use them. Because, PHP is a server-side development technology. It is best when doing that work and rarely goes beyond these boundaries.

A few years ago, JavaScript was often considered more restrictive. There are several other beard technologies but its main function is to run in browsers. Node.js changed that perception and had an explosion of JavaScript projects. You can use JavaScript everywhere – in the browser, on servers, terminals, desktops and even embedded systems. Node.js has made JavaScript popular.

Game 6: Integration

Development technologies will be limited unless they can integrate with databases and drivers. PHP is very strong in this area. It has been developed for years and its extended systems allow direct communication with a variety of popular or obscure APIs.

Node.js is growing very fast, but you may have to work very hard to find integrated components that are mature enough for older and less popular technologies.

Session 7: Hosting and deployment

Which one is easier to deploy your new application to on a web server? Obviously the winner is PHP . Randomly select a few web hosting companies and contact them, you will discover that most of them support PHP. You will be able to get additional promotions with MySQL at a bargain price. PHP is considered easier to test and can help reduce many risks.

Node.js is a completely different creature and server-side applications can operate in a long-term way. You will need a real / virtual / cloud or specialized server environment, ideal for SSH root access. It is a too high requirement for some providers, especially on shared hosting, where you can crumble the whole system.

Node.js hosting will become simpler, but I doubt it will ever be as easy as using FTP to upload some PHP files.

Game 8: Performance

PHP is not too heavy and has projects and options that make it faster. Even the most demanding PHP developers rarely worry about speed, but the performance of Node.js is generally better. Of course, performance is largely the result of the experience and care taken by the development team, but Node.js has many advantages …

Less dependencies
All requests to a PHP application must be routed through a web server and the PHP interpreter will start running that code. Node.js does not need too many dependencies, while you will almost certainly use a server framework like Express , it is quite light and is part of your application.

The interpreter is smaller and faster
Node.js is smaller and faster than the PHP interpreter. It is less hindered by legacy language support and Google has invested heavily to enhance the performance of V8 engine.

The application is Permanently On
PHP follows the typical client-server model. Every page request initializes your application; You load configuration parameters, connect to a database, get information and export to HTML. A Node.js application runs permanently and it only needs to be initialized once. For example, you can create a single database connection object and be reused by everyone in every request. Admittedly, there are many ways to do the same thing in PHP using systems like Memcached but it is not a standard feature of this language.

An event-driven, Non-Blocking I / O
PHP and other server-side languages ​​use a clear blocking enforcement model. When you issue a command such as getting information from a database, the command will complete before proceeding to the next statement. Node.js will not wait. Instead, you provide a callback function that will be executed once that action completes, for example:

In this example, the console window will output ‘finished’ before ‘N records returned’ because the function process is called when all data has been retrieved. In other words, the interpreter is free to do other things while other processes are busy.

Note that there are many complex situations and differences:

  • Node.js / JavaScript runs on a single thread, while most web servers are multi-threaded (multi-threaded) and handle requests simultaneously.
  • JavaScript processes that run too long for a user will prevent the code from running for all other users, unless you split up multiple tasks or use Web Workers.
  • The speed benchmark is subjective and flawed; You will find examples that Node.js defeats PHP and the opposite examples are PHP that defeats Node.js. Professional developers always test anything they trust!
  • Writing asynchronous event-driven code is complex and must bear its own challenges.

I can only go from personal experience: my Node.js application is significantly faster than equivalent PHP applications. Your experience may not be the same but you will never know until you try it.

Game 9: Programmer’s passion

This can be expanded into a “general web development challenge” and it is important. A technology that is good or bad will not matter if you are afraid to write code every day.

It’s a bit difficult to compare but relatively few PHP developers are passionate about the language. When was the last time you read an article about PHP or saw a presentation that captivated the audience? Perhaps everything has been told? Perhaps there is too little disclosure? Maybe I didn’t find the right place? There are some nice features in the upcoming PHP7 version but this technology has been soaked in for a few years. That said, some PHP developers have blamed the language.

JavaScript contributes to breaking up the community. There are people who love it and those who hate it; some developers of “three must” do not take sides. However, the feedback on Node.js is largely positive and the technology is riding on the waves. This is partly because it is new and compliments cannot last forever, but now Node.js will win this round.

Game 10: The future

Which server-side language you use is not particularly important; it will continue to work even if the development project is canceled (eg ColdFusion!). Usage may be different but many people continue to use PHP. People bet on its safety and are assured of seeking support for another 20 years.

The rise of Node.js is like a wind. It provides a modern development approach that uses the same syntax as client-side development and supports revolutionary HTML5 features like web sockets and server-sent events. Now there has been some confusion about branching of this language, but its community continues to increase exponentially.

Node.js will definitely eat into PHP’s market share but I doubt it will even pass. Both technologies have a bright future. I declare this match.

Final victory

Final score: Node.js wins in 5 innings, PHP wins 4 innings and has a draw. The result is closer to my expectations and may have one of two ways.

Node.js is quite difficult to learn and is not ideal for new web developers but it wins in this game. A very worthy victory. If you are a competent JavaScript programmer and have loved this language, then Node.js doesn’t disappoint you. It brings a fresh feeling and provides a liberal web development experience – but you won’t miss PHP.

And don’t look down on it. PHP is alive and there is little reason to jump on the Node.js boat just because it looks faster, newer, or more trendy. PHP is easier to learn and supports professional programming techniques effectively. Help is everywhere and its implementation is quite simple. Even Node.js’s “guts” developers should consider PHP for simple websites and applications.

My advice is: evaluate your options and choose a programming language based on your requirements. That would be much more practical than relying on soccer posts like this!

Do you agree with the author’s conclusion? Is it fair to compare like that? Are there any mistakes in the innings? Don’t be angry – write down your point of view in the comment section below …

ITZone via Techmaster

Share the news now

Source : Techmaster