Embreo - Mobile Application Development Studio
Mobile Application Development Studio
01.png

Blog

PHP vs Node.Js Why Choose the latter?

It’s a classic Hollywood plot: the battle between two old friends who went separate ways. Often the friction begins when one pal sparks an interest in what had always been the other pal’s unspoken domain. In the programming language version of this movie, it’s the introduction of Node.js that turns the buddy flick into a grudge match: PHP and JavaScript, two partners who once ruled the internet together but now duke it out for the mind share of developers.

In the old days, the partnership was simple. JavaScript handled little details on the browser, while PHP managed all the server-side tasks between port 80 and MySQL. It was a happy union that continues to support many crucial parts of the internet. Between WordPress, Drupal, and Facebook, people can hardly go a minute on the web without running into PHP.

Then some clever kid discovered he could get JavaScript running on the server. Suddenly, there was no need to use PHP to build the next generation of server stacks. One language was all it took to build Node.js and the frameworks running on the client. "JavaScript everywhere" became the mantra for some.

Since that point, both platforms have continued to expand and improve. Node.js developers can now choose between an ever-expanding collection of excellent frameworks and scaffoldings: Express, Angular, Meteor, and more. The list is long and the biggest problem is choosingbetween excellent options.

PHP is also speeding up, with a zippy, just-in-time compiler that delivers answers faster than ever thanks to the same techniques that powered the Node.js revolution. Now PHP 7.0 and the HHVM offer many of the same clever on-the-fly optimizations that V8 brought to Chrome and Node.js. Not only that, but HHVM has Hack, a clever language that offers full support for sophisticated programming features such as lambdas, generics, and collections.

Of course, the ending isn't written yet. For every coder crowing about the purity of Node.js and the simplicity of JavaScript everywhere, there's another who's happy with the deep code base and long-understood stability of PHP. Will the old codger beat back the server-side upstart? Will JavaScript topple its old friend to achieve world domination? Put another batch of popcorn in the microwave and sit back.

Where PHP wins: Mixing code with content

You're typing along, pouring thoughts into text for your website, and you want to add a branch to the process, a little if-then statement to make it look pretty, say, depending on some parameter in the URL. Or maybe you want to mix in text or data from a database. With PHP, you open up the magic PHP tags and start writing code within seconds. No need for templates—everything is a template! No need for extra files or elaborate architectures, only programmable logistical power at your fingertips.

Where Node wins: Separating concerns

Mixing code with content is a crutch that can end up crippling you. Sure, it's fun to mix code in with HTML the first two or three times you do it. But soon your code base becomes a tangled mess of logic. Real programmers add structure and separate the cosmetic layer from the logical layer. It's cleaner for new programmers to understand and easier to maintain. The frameworks running on Node.js are built by programmers who know that life is better when the model, view, and controller are separate.

One of the key drivers for enterprise cloud adoption in APEJ is the migration or expansion of existing on-premises workloads into the public cloud. Find out if you’re well-placed to ride this wave...

Where Node wins: Newer code means more modern features

Sure, there are thousands of great open source PHP files, but some are 8-year-old WordPress plugins hoping and praying that someone will download them. Who wants to spend hours, days, or weeks monkeying with code that hasn’t been updated in years? Node.js plugins are not only newer, they were built with full knowledge of the latest architectural approaches. They were built by programmers who understand that modern web apps should push most of the intelligence to the client.

Where PHP wins: Simplicity (sort of)

There's not much to PHP: a few variables and basic functions for juggling strings and numbers. It's a thin layer that doesn't do much except move the data from port 80 to the database and back. That's what it's supposed to do. A modern database is a magical tool, and it makes sense to leave the heavy lifting to it. PHP is the right amount of complexity for a job that's not supposed to be complex.

Where Node wins: Complexity of closures and more

JavaScript may have many little idiosyncrasies that drive some mad, but for the most part it is a modern language that sports a modern syntax and a few useful features like closures. You can reconfigure and extend it easily, making powerful libraries like jQuery possible. You can pass functions around like objects. Why limit yourself?

 

Where Node wins: Dozens of language options

If PHP users are happy to get access to Hack, they should consider moving to the world of Node.js because many major languages can be cross-compiled to run in JavaScript. There are well-known options like Java, C#, or Lisp and dozens of others like Scala, OCaml, and Haskell. There are even gifts for nostalgic lovers of BASIC or Pascal. This list from Jeremy Ashkenas is fairly comprehensive.

Where Node wins: Service calls are thinner than HTML-fat PHP calls

While AJAX-crazy HTML5 web apps can have too many moving parts, they are cool—and very efficient. Once the JavaScript code is in the browser cache, the only thing that moves along the wires is the new data. There's not a ton of HTML markup, and there are no repeated trips to download the entire page. Only the data has changed. If you're willing to put in the time to create a slick browser-side web app, there's a big payoff. Node.js is optimized to deliver the data and only the data through web services. If your app is complex and data-rich, it's a good foundation for efficient delivery.

Where Node.js wins: JSON

If you must have access to SQL, Node.js has libraries to do that. But Node.js also speaks JSON, the lingua franca for interacting with many of the latest NoSQL databases. That’s not to say you can’t get JSON libraries for your PHP stack, but there's something fluid about the simplicity of working with JSON when using JavaScript. It's one syntax from browser to web server to database. The colons and the curly brackets work the same way everywhere. That alone will save you from hours of frustration.

Where PHP wins: Speed of coding

For most developers, writing PHP for web apps feels faster: no compilers, no deployment, no JAR files or preprocessors—simply your favorite editor and some PHP files in a directory. Your mileage will vary, but when it comes to banging a project together quickly, PHP is a good tool to use.

Where Node.js wins: Raw speed

Writing JavaScript code is a bit harder when you're counting curly brackets and parentheses, but when it's done, your Node.js code can fly. The callback mechanism is brilliant because it saves you from juggling the threads. The core is well-built and designed to do all that for you. Isn't that what everyone wants?

Where Node.js wins: Solidarity

Do you really want two different code bases? Sure, competition helps, but fragmentation soon follows. What happens when your code runs on only one of the two? Competition doesn’t do any good if you have to spend weeks or months rewriting your code. While Node.js experienced its own splintering a few years back, with the launch of io.js, the Node.js universe has since reunited, giving it the kind of language solidarity that PHP developers may soon long for.

leoda embreo