Originally published at https://www.geeksforgeeks.org
Both platforms have access to the command line interface via:
Example: Printing ‘Hello World’ in PHP and Node.js
The following snippets compare the print ‘Hello World’ program in both the languages:
PHP
// Printing Hello GeeksforGeeks in PHP echo 'Hello GeeksForGeeks';
Node.js
console.log('Hello GeeksForGeeks');
Note: To run the Node.js code, please use the REPL environment.
Synchronous code executes line by line and proceeds to execute the next line of code when the current line has been executed.
Asynchronous code executes all the code at the same time.
Note: Program can get stuck in a ‘callback hell’ if a lot of functions needs to be chained which might require piping data from one function to another. However, it can be resolved by Node.js as it has feature of Async/Await which can help a block of code execute synchronously.
The Switch between different environments and languages is attributed to the drop of efficiency when writing code. Changing between multiple coding languages leads to drop in the efficiency of the programmer.
Example: Laravel framework
// requires Composer installed on your system // run following command on terminal. // This installs laravel on your system composer global require "laravel/installer"// Below command creates a folder called
// GeeksForGeeks with laravel installed
laravel new GeeksForGeeks
Example: Express framework web server:
// Below command installs ExpressJS
// in your project folder
npm install express --save// creating web server using Express framework
// write the following code in your gfg.js filevar express = require(‘express’);
var app = express();
express.listen(‘3000’, function(){
console.log(’ GeeksForGeeks demo server
running on express’);
});
Negative point PHP: MySQL database systems are especially prone to SQL injection attacks, Cross-side scripting(XSS) and others.
Negative point Node.js: Even though they are not that common, NoSQL injection attacks are a documented vulnerability. But compared to SQL injection, they are negligible. The major reason for this is that they are new and their code design is in such a way that they are inherently resistant to such attacks.
Example: Starting PHP server
// starting php server
$ php -S localhost:8000// index.js file code
<?php
echo ‘Hello! This is GeeksForGeeks’;
?>
The PHP web server was provided to aid application development and can’t be used efficiently as a full-fledged web server.
Example: Starting Node.js server
// starting Node.js server
$ node app.js// app.js source code var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200, { ‘Content-Type’ : ‘text/plain’ });
res.end(‘Hi Programmer\n’);
})
.listen(8080, ‘127.0.0.1’);
console.log(‘GeeksForGeeks Server running at http://127.0.0.1:8080/’);
Own web servers can be coded in Node.js on which Node.js applications can run. These servers have the potential of high scalability if configured and monitored properly.
Note: PHP should be used in applications in which client does not have to interact with the server again and again and Node.js should be used for the applications which require a lot of interaction between client and server.
#php #node-js #web-development