Home php教程 php手册 简单谈谈PHP vs Node.js

简单谈谈PHP vs Node.js

Jun 06, 2016 pm 07:51 PM
node.js php

这篇文章主要介绍了简单谈谈PHP vs Node.js,需要的朋友可以参考下

网络正在处于一个日新月异的发展时代。服务器端开发人员在选择语言的时候非常困惑,有长期占主导地位的语言,例如C、Java和Perl,也有专注于web开发的语言,例如Ruby、Clojure和Go。只要你的项目运行良好,你的选择就显得没有那么重要了。

简单谈谈PHP vs Node.js

  但是如何让这些新的web开发人员做出一个正确的选择呢?

  我不希望展开一场PHP、NodeJs两大阵营之间的战争,我将会对比这两种语言所在领域的发展状况:

PHP

Rasmus Lerdorf在1994年创造出了PHP。它是由安装在web服务器(Apache、Ngix)上的组件运行的。

PHP代码可以和HTML混合到一块。 对于初学者就能很快写出很有价值的代码,这并不需要太多的练习。 这让PHP变得越来越流行, 现在全球百分之八十的服务器上都在运行着PHP。全球四分之一的网站都在用的一个内容管理系统--WordPress,就是用PHP写的。

Node.js

Ryan Dahl在2009年创造了Node.js。它是基于Google的V8 JavaScript解释引擎(在Chrome浏览器里它负责执行客户端的JavaScript代码)。与其它语言不同的是Node.js内置了处理网络请求和响应的函数库,所以你不需要单独的服务器(Apache、Ngix)或者其他依赖。

Node.js虽然很新但是很快就获得了极大的追捧。在很多的大公司都有在使用,例如:Microsoft,Yahoo,LinkedIn和PayPal。

  我们所钟爱的C#、Java、Ruby、Python、Perl、Erlang、C++、Go、Dart、Scala、Haskell等等,它们呢?
  如果文章里边比较了如上所有语言的各种参数,文章将会非常长,你还会读它么?你期望一个程序员了解所有的程序开发语言吗?这显然是不可能的。我主要对PHP和Node.js做了比较,主要原因如下:

首先它们之间值得比较。两者都是开放源码,都是致力于web开发,并且都可用于相似的项目。

PHP已经发布了很久了,但是Node.js才刚刚兴起,并受到越来越多的关注。难道PHP的程序员应该相信Node.js的宣传?应该考虑转换语言?

我了解,热爱编程语言,自从20世纪90年代之后我就一直使用PHP和JavaScript,并且也有几年的Node.js的经验。除此之外我也涉猎了其他的技术,但是在这里我还不能对他们作出客观评价。

  另外,对多少语言做比较不重要,因为总会有某些地方的某些人抱怨我没有提到他们语言。

  SitePoint上的角逐

  程序员会花费很多时间来提升他们自己的编程能力。一些人有编程语言之间的延伸能力,但是这些达到更高层次的人根据很多的因素来做出他们自己的选择。从主观方面来说,你将会推进并且捍卫你的技术决策。

  SitePoint Smackdowns并没有采取“选择适合你自己的,朋友”这样的观点。我将会根据个人的经验、要求和喜好提出建议。你可能不会同意所有的我所说的所有观点,这并不重要,重要的是你的意见将会给其他人做出更加明智的选择。

  评估方法

  下面将会对 PHP 和 Node.js 进行十个轮次的比较。每个回合都会考量可以应用到任意 web 技术的常见的开发挑战。我们不会太深入讨论细节;很少有人会关心随机数生成器或数组排序的价值。

  赢得最多回合的将会是优胜者。准备好了吗?开始较量吧。。。。。。

  第一轮:起步

  创建一个“Hello World” web 页面有多快呢?在 PHP 中:

  这段代码可以放置在可以被 PHP 引擎解析的任意文件中--通常,是以 .php 为后缀的文件。在浏览器中输入 URL 跳转到文件即可。

  不可否认,这不是全部。这段代码只能运行于安装了 PHP 的 web 服务器(PHP 有一个内置的服务器,尽管如此,最好还是使用一个更健壮的服务器)。大多数操作系统提供了服务器软件,如 windows 上的 IIS,Mac 和 Linux 上的 Apache,尽管它们需要启动和配置。通常使用提前构建的安装程序,如 XAMPP或虚拟机镜像(如 Vagrant)。一种更简单的方式:上传你的文件到任意的 web 主机。

  对比而言,安装 Node.js 轻而易举。你可以 下载安装程序 或者 使用包管理器。 接下来让我们在 hello.js 中创建 web 页面:

var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World!'); }).listen(3000, '127.0.0.1');

  在浏览器中访问 :3000/  之前,你需要在终端输入 node hello.js 来启动应用程序 。通过上面的五行代码,,我们已经创建了一个小型的 web 服务器,尽管这很令人吃惊,但是即便拥有很强客户端 JavaScript 经验的人也很难理解。

  PHP 在概念上更简单所以赢得本轮。稍微懂得一些 PHP 声明的人就可以开发一些有用的东西。PHP 有更多的软件依赖,但是 PHP 的概念对于新手来说不那么繁琐。

  懂一些 JavaScript 和开发 Node.js 应用是两回事儿,Node.js 开发方法和大多数服务端技术不同,你需要先弄明白一些相当复杂的概念,比如关闭和回调函数。

  第二轮:帮助和支持

  没有官方文档和资源(课程,论坛,堆栈溢出)的帮助你必将举步维艰。 PHP 在本轮轻易胜出,她有大量的指南和二十年的 Q&A。无论你想做什么,总会有人在你之前已经面对过同样的问题。

  Node.js 拥有很好的文档,但是更加年轻,能提供的帮助较 PHP 也少。JavaScript 在市面上的时间和 PHP 一样久,但是主要的帮助都是针对浏览器开发的,那基本没啥帮助。

  第三回合: 语言语法

  声明与结构是不是符合逻辑而且简单好用?

  不像一些语言跟框架,PHP 不会限制你按特定的方式编写,具体怎么搞随你。你可以从几行的程序开始,然后添加些方法,进而写一些简单的 PHP4 模式的对象,最后编写优雅的面向对象的 MVC 模式 PHP5+ 的应用。你的代码开始可能比较混乱,但也能工作,而且会随着理解的深入越写越好。

  PHP 的语法在版本间可能略有调整,但是向后兼容一般都做得很好。但不幸的是,这也导致了一个问题:PHP 很混乱。例如,怎么统计一个字符串中字符的个数?是 count?str_len? 还是 strlen?亦或 mb_strlen?PHP 有数以百记的函数,而且命名规则也也不完全一致。可以试试不查文档写几行代码。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

See all articles