Home > Article > Backend Development > A multi-faceted analysis of the php vs node.js debate
The debate between PHP vs Node.js has been going on for a long time. The former is used for dynamic web development, while the latter is a JavaScript toolkit used to write high-performance web servers. How do they compare? Peter Wayner of the InfoWorld Test Center recently wrote an article pointing out the advantages of the two. You might as well take a look.
The following is the translation:
This is a typical Hollywood plot: a battle between two old friends who have gone their separate ways. Friction often begins when one person is interested in areas where the other person is self-evident. The film’s programming language version is that the introduction of Node.js turns the friends plot into a close match. PHP and JavaScript, two partners who once ruled the Internet together, are now vying for a share of developer hearts.
In the past, their partnership was simple. JavaScript handles the small details on the browser, and PHP handles all the server-side tasks that exist on port 80 and MySQL. This happy union continues to power many key parts of the Internet. On WordPress, Drupal, and Facebook, people hardly leave PHP for a minute.
But then some smart kid figured out he could make JavaScript run on the server. Suddenly, we realized there was no need to build the next generation server stack in PHP. One language is enough to build Node.js and the framework that runs on the client side. For some, "JavaScript is everywhere" has become a mantra.
Of course, the ending is not finished yet. Compared to the simple programmers who tout the purity of Node.js and the ubiquity of JavaScript, there are programmers who are satisfied with the depth of the code base and the stability of understanding PHP. Can the strange old man defeat the server-side upstart? Can JavaScript overthrow its old friend and achieve world domination? Let's grab another handful of popcorn in the microwave, sit down and take a look.
Where PHP Wins: Mixed Content Code
You are typing, and the thoughts become the text on your website. You want to add a branch to the process, based on some parameters of the URL, a little if-then statement will make it look nice. Or maybe you want to add text or data from a database. With PHP, you can open the PHP magic tab and start writing code in seconds. No templates required - everything is a template. No extra documentation or painstaking architecture is required because the power of programmable logic is at your fingertips.
Where Node Wins: Separated Content
Code that mixes content is a crutch that will ultimately hurt you. Of course, mixing HTML code is fun the first two or three times. But soon, your code base becomes a mess. Real programmers add structure, separating the decorative layer from the logical layer. For new programmers, the code is easy to understand and maintain. The frameworks that run on Node.js are built by programmers who know that life is better when models, views, and controllers are separated.
Where PHP wins: Deep code base
The web is full of PHP code. The most popular platforms for building websites (WordPress, Drupal, and Joomla) are all written in PHP. Not only these open source platforms, most of their plug-ins are also written in PHP. There is PHP code everywhere on the Internet, waiting for you to download, modify and use it for your own use.
Where Node Wins: New Code Means More Modern Features
Of course, there are thousands of open source PHP files online, but some are 8-year-old WordPress plugins hoping and praying that someone downloads them. Who wants to spend hours, days, or weeks messing around with code that hasn't been updated in years? Node.js plugins are not only new, but built with complete knowledge of the latest architecture.
Where PHP wins: Simplicity (to a certain extent)
There isn’t much in PHP: a few variables and basic functions for handling strings and numbers. It doesn't do much other than move data from port 80 to the database and back. This is what should be done. Modern databases are amazing tools that take away heavy loads. PHP has the right amount of complexity for work that shouldn't be complicated.
Where Node wins: Closures and more complexity
JavaScript can have a lot of little quirks that drive some people crazy. But for the most part, it's a modern language with an entertaining modern syntax, and has several useful features, such as closures. You can easily reconfigure and extend it, making powerful libraries like jQuery possible. You can pass functions around just like objects. Why limit yourself?
Where PHP wins: no client application required
All this talk about using the same language in the browser and server is good, but what if you don’t need to use any language in the browser? What if you send data in an HTML form? Browser pop-ups, without the headaches and glitches caused by unactivated JavaScript trying to create a page on the browser from over twenty web service calls. Pure HTML works more often than anything else, while PHP is optimized for creating HTML. Why bother using JavaScript in your browser? Build all operations on the server to avoid small browser reloads on small phones.
Where Node wins: Thin service calls as opposed to HTML-fat PHP calls
While AJAX-crazy HTML5 web applications have a lot of moving parts, they are cool and very effective. Once the JavaScript code is in the browser cache, new data moves down the line. This doesn't involve a lot of HTML markup and downloading the entire page repeatedly. Only the data has changed. If you're willing to invest the time in creating a slick browser-side web application, it will pay well. Node.js is optimized to send data, only data through the web service. If your application is complex and data-rich, this will be a good basis for efficient delivery.
Where PHP wins: SQL
PHP vs. MySQL and its many variants, such as MariaDB. If MySQL doesn't get it all right, there are other great SQL databases from Oracle and Microsoft. Your code can be converted into your query with very few changes. The vast world of SQL has no boundaries. Some of the most stable, mature code interfaces with SQL databases, meaning all the power can be easily integrated into PHP projects as well. It may not be the perfect happy family, but it's big.
Where Node.js wins: JSON
If you must access SQL, Node.js’s function library can do it. But Node.js also has JSON, a common language for interacting with many of the latest NoSQL databases. That's not to say you can't get a JSON library for your PHP stack, but there are some fluids when using JavaScript that can be handled using the simplicity of JSON. This is the syntax from the browser to the web server to the database. Colons and braces work the same everywhere, which saves you time.
Where PHP wins: Coding speed
For most developers, writing PHP web applications feels faster: no compiler, no deployment, no JAR files or preprocessors - just your favorite editor and some PHP file directories. Your pace will vary, but in terms of quickly finalizing projects together, PHP is a great tool to use.
Where Node.js Wins: Raw Speed
In the process of writing JavaScript code, it is a little difficult when you count the curly braces and parentheses, but after successful writing, your Node.js code can fly. Its callback mechanism is very clever because it can help you save time running threads. The core is built to do everything for you, isn't that what everyone wants?