📜  PHP与 Node.js

📅  最后修改于: 2022-05-13 01:56:31.082000             🧑  作者: Mango

PHP与 Node.js

PHP和 Node.js 都用于服务器端开发,因此已成为彼此的竞争对手。以下是基于不同参数的一些差异,以了解两者并在两个巨头之间做出决策。

PHP与 Node.js

PHPNode.js
PHP is an acronym for Hyptertext Preprocessor created by Rasmus Lerdorf in 1994. PHP is an open-source server-side scripting language designed specifically for the web development. Although PHP is a server-side scripting language, it is also used as a general purpose scripting language. PHP scripts have an extension of .php and can contain Javascript, HTML, CSS and even plain text.Node.js is an open-source server-side Javascript run-time environment built on Chrome’s JavaScript Engine(V8). Node.js is used for building fast and scalable applications and is an event driven, non-blocking I/O model. Node.js files have .js extension and contain only Javascript. It’s original author is Ryan Dahl and was initially released on May 27, 2009. With the birth of Node.js, it brings users the facility to built completely Javascript based applications.

语法和命令行访问

两个平台都可以通过以下方式访问命令行界面:

PHPNode.js
$ php -i$ node

示例:在PHP和 Node.js 中打印“Hello World”
以下片段比较了两种语言的 print 'Hello World' 程序:

PHP
// Printing Hello GeeksforGeeks in PHP
echo 'Hello GeeksForGeeks';  
Node.jsconsole.log('Hello GeeksForGeeks');


PHP
// starting php server
$ php -S localhost:8000
 
// index.js file code


Javascript
// 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/');


注意:要运行 Node.js 代码,请使用 REPL 环境。

同步或异步

同步代码逐行执行,并在当前行执行完毕后继续执行下一行代码。
异步代码同时执行所有代码。

PHPNode.js
PHP is synchronous but there are some APIs that behave asynchronously apart from the synchronous lot. The problem with being synchronous can be understood by a simple example. Suppose, the first line of code has a function that takes a lot of time to execute. Now due to synchronous nature, the below lines of code has to wait for their turn and will execute only after the function is executed. This makes it slower and the user to wait.Node.js is asynchronous in nature which means the JavaScript engine runs through the entire code in one go and does not wait for a function to return. The lines of code below the function will execute and the function be executing too and will return the output once done and thus it make Node.js fast.

注意:如果需要链接许多函数,这可能需要将数据从一个函数传递到另一个函数,程序可能会陷入“回调地狱”。但是,它可以通过 Node.js 解决,因为它具有 Async/Await 功能,可以帮助代码块同步执行。

上下文切换

不同环境和语言之间的切换归咎于编写代码时效率的下降。在多种编码语言之间切换会导致程序员的效率下降。

PHPNode.js
Writing back end code in PHP, user continuously switches between different language and syntax. This is because PHP is predominantly used as part of LAMP stack which includes MySQL (for database), PHP (for server-side code), and linux. All of them have different syntax plus good knowledge of HTML, CSS and Javascript is required.Since Node.js is written in JavaScript, it makes both the sides server-side and client-side based on JavaScript so there is no need to switch between the languages. Javascript stack(MEAN or MERN) is better because the only coding language and syntax used is Javascript based. 
 

模块

PHPNode.js

PHP uses module installing technologies like PEAR(a veteran package system), and Composer which is comparatively new. 
 

  • PEAR is a framework and distribution system for reusable PHP components.
  • Composer is a tool for dependency management in PHP. It allows users to declare the libraries on which the project depends and it will manage (install/update) them for user.
Node.js comes bundled with a package management system called NPM (Node Package Manager) and its registry which is easy to use and publish.

构架

PHPNode.js
PHP is a very popular server-side scripting language and has many frameworks which help in easy backend development. Some of them are Laravel, CodeIgniter, Cakephp, etc. These frameworks help in agile, robust, and secure backend development of web applications.Frameworks like Express and the full-stack MVC frameworks Meteor and Derby are the most popular. New frameworks keep popping up every now and then like koa.js, hapi, total.js, sails.js, etc. 
 

示例: Laravel 框架

// 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

示例: Express 框架 Web 服务器:

// 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 file

var express = require('express');
var app = express();
express.listen('3000', function(){
console.log(' GeeksForGeeks demo server
                  running on express');
});

数据库

PHPNode.js
PHP is used in collaboration with traditional/relational databases like MySQL, MariaDB, PostgreSQL, etc. However, there are ways to use NoSQL database systems with PHP too but they are not very popular.Node.js works perfectly with NoSQL (Not only SQL) databases like MongoDB, CouchDB, and graph database systems like Neo4j. The NPM packages for almost all the databases are available on the npm registry.

缺点PHP: MySQL 数据库系统特别容易受到 SQL 注入攻击、跨端脚本 (XSS) 等攻击。

负面观点 Node.js:尽管它们并不常见,但 NoSQL 注入攻击是一个记录在案的漏洞。但与 SQL 注入相比,它们可以忽略不计。造成这种情况的主要原因是它们是新的,并且它们的代码设计方式使它们天生就可以抵抗此类攻击。

网络服务器

PHPNode.js
For versions prior to 5.4, LAMP and XAMPP(acronym for Cross-platform, Apache, MariaDB, PHP) servers had to be setup. 
But from v5.4, PHP comes with a built-in development server which can be used.
Nodejs was developed for the network applications. It ships with some core modules like http, DNS, file system, etc. which helps to develop customized web servers. Some really popular frameworks for powering Node.js running web servers are Express.js, koa.js and Sails.js which can be setup by using only 4 lines of code at max.

示例:启动PHP服务器

PHP

// starting php server
$ php -S localhost:8000
 
// index.js file code

PHP网络服务器是为了帮助应用程序开发而提供的,不能有效地用作成熟的网络服务器。

示例:启动 Node.js 服务器

Javascript

// 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/');

可以在 Node.js 中对自己的 Web 服务器进行编码,Node.js 应用程序可以在其上运行。如果配置和监控得当,这些服务器具有高度可扩展性的潜力。

应用领域

PHPNode.js
  • Used in developing CPU-intensive applications like meteorology applications and scientific applications.
  • LAMP stack is used in API development.
  • CMS (Content Management Systems) like WordPress, Drupal also use PHP which makes it possible to be used in creating blogs, websites, e-commerce sites etc.
  • Nodejs is ideal for developing highly scalable server-side solutions because of its non-blocking I/O, and event-driven model.
  • Used massively in Real-time applications like chat applications, blogs, video streaming applications.
  • Used in developing single-page applications like resume portfolios, individual websites.

注意: PHP应该用于客户端不需要反复与服务器交互的应用程序中,而 Node.js 应该用于需要客户端和服务器之间进行大量交互的应用程序。