Table of Contents
回复内容:
Home Backend Development PHP Tutorial JavaScript 和 PHP 两种编程语言哪个设计得更好(差)一点?

JavaScript 和 PHP 两种编程语言哪个设计得更好(差)一点?

Jun 17, 2016 am 08:31 AM
javascript php

以前断断续续学过很长时间的javascript,已经模糊了当年的痛苦;如今在学php(看网页+实践,没看书),感觉php特别难用。也想起javascript和php都是被评价为设计糟糕的语言,因此想起了这个问题。

回复内容:

首先声明:我曾经写过两年PHP,对语法还算熟悉,对核心API 始终半生不熟(因为太杂乱难记),我欣赏PHP 的简单粗暴流行,喜欢PHP 的各种框架,但对PHP 的语法始终爱不起来。

=============

个人更喜欢 js 的语法和核心 API,变量取名不需要花钱,函数第一型,API 好记,最重要的是有规范有游戏规则。

当然 js 也有槽点,callback hell 糟糕了点,iterator 的引入迟了点,class语法不够甜,等等等等,让 js 在一些事情上不如 PHP 好用,但还好一切都是可以慢慢改正的。

=================================

PHP 一开始就糟糕:

变量名的限制,使我一看到PHP就开始心疼键盘;

是变量名不区分大小写,还是函数名不区分大小写来着?

糟糕的核心API 命名,更糟糕的扩展API 命名,我写PHP 的那两年,随身带着一本 PHP Manual,连最常用的字符串处理方法也不能达到熟练的程度,永远在怀疑自己应该用下划线命名法还是驼峰命名法还是干脆全小写字母,永远在怀疑自己应该用过程式的API 还是oop 式的API;

在lambda引入之前,PHP 连简单的 string.replace(regex, fn) 都磕磕绊绊,很多时候我想要把函数传来传去,结果不是在用字符串创建函数,就是用把函数名写到字符串里传过去,蛋疼得很;

新引入的匿名函数跟传统的命名函数,表现和用法有着十分的不同;

没有 scope chain,糟糕的 global,繁琐的闭包 use,一切都像是在一坨排泄物上费尽心思打补丁,打得还如此丑陋。

=================================

我对 PHP 语法和API 的看法就是,与其打上这么厚的补丁,不如推倒重来?不过那还能叫 PHP 吗。。 JS和PHP都有很多设计失误。

不同的是,JS的进化填了旧坑,并且几乎没有任何新坑。而PHP虽然每个版本也都在填坑,但是总是填不干净,还会产生新坑。


【补充】
即使看最初的设计,JS当然也比PHP好。Brenden Eich虽然只花了10天设计实现,但是比Rasmus还是高段多了。Rasmus自己都说是随便玩玩,根本不是奔着设计语言去的。
语言特性上,只说一点。JS有闭包,就足够支撑它在有诸多重大缺失情况下,作出许多精妙的库和框架。而PHP直到现在虽然加了闭包,却如鸡肋般难用。

【又补充】
@鲁小夫 吐槽了PHP大小写规则,评论里有人说什么“php遵循的是unix风格,即变量是区分大小写的,函数不区分大小写”。恕我从没听说过这种“unix风格”。
PHP的大小写规则的来历见我写的这篇:PHP 为什么大小写规则是如此不规则? · Issue #29 · hax/hax.github.com · GitHub 难道你们已经进化到需要关心语言的坑了么?不是只有语法不一样? PHP 最开始的版本是一种模板语言。。。 可能是时代不同需求也不同吧,PHP诞生的初衷是创造一种快速制作动态网页的语言,PHP破天荒的将代码嵌入HTML中执行,而不是拼字符串。PHP创始人当时可能抠破脑皮也没想到,现在的人会流行搞MVC、忙着把视图代码和逻辑代码分开。PHP在当时制作动态网页的技术中,理念是相当成功的,所以一炮而红。PHP本身就是为动态网页而生的,同时造成了PHP和其它语言相比可能许多地方比较另类,以至于PHP每次迭代都在试图向主流通用型语言发展,说得好听叫成长,说得不好听就叫填坑吧。 一个是Personal Home Page(好吧就算最新的递归缩写也只是PHP: Hypertext Preprocessor
一个是JavaScript
从取名就能看出来他们自己的定位。

所以现在JS成了一个通用的中间语言了,PHP还是只能做Web。

其实说起来,Brendan Eich在实现第一版JS的时候还是考虑过设计的,但是PHP,呵呵,并没有看出来除了提升版本号和打补丁之外还考虑过什么。

当然,Hack/HHVM作为一个后继者,狠狠滴打了PHP社区的脸,然而没推广起来就没什么卵用。
所以现在看看PHP,要啥有啥(Hack甚至还加入了Generics和ADT),俨然是一副高端编程语言的样子。但是要啥啥丑,就像是一个家里不怎么有钱的丑逼官二代。

然后你看JavaScript,经过Andres Hejlsberg和Erich Gamma的手就变成了神器了。

至于推翻重来,你们是不是没有听说过PHP 6和ECMAScript 4啊? 这是js被黑的最惨的一次 从语法上说,JavaScript胜出,尤其是引入JSON以后。
从接近C和Java的角度来说,PHP更容易入门。

如果抛开各种库,
JavaScript很精简,只有很少的内置对象,却能满足大部分编程序求。
PHP显得臃肿,各种内置函数,命名还不一致。 PHP是世界上最好的语言! 虽然说我是坚定不移的php黑,但题主你这黑的一点道理都没有,刚学就能知道php难用?你能说出哪难用吗?
学习的时候不要先入为主行吗,php唯一优点就是上手快(我又黑了一波)
是不是一个语言要复杂的像c艹你才不明觉厉?
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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

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,

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

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.

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

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.

See all articles