PHP & 区分大小写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33273941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:26:08  来源:igfitidea点击:

PHP & Case Sensitivity

phpcase-sensitivecase-insensitive

提问by Manngo

In PHP, variable and constant names are case sensitive, while function names are not.

在 PHP 中,变量名和常量名区分大小写,而函数名不区分大小写。

As far as I am aware, PHP is the only language in which this happens. All other languages I have used are either totally case sensitive or totally case insensitive.

据我所知,PHP 是发生这种情况的唯一语言。我使用过的所有其他语言要么完全区分大小写,要么完全不区分大小写。

Why is PHP partiallycase senstive?

为什么 PHP部分区分大小写?

Please note, that I am not asking whichnames are case sensitive, but why.

请注意,我不是问哪些名称区分大小写,而是问为什么.

Update

更新

I thought I might add, for the benefit of those who think I am asking which, the following list:

为了那些认为我在问哪个的人的利益,我想我可以添加以下列表:

Case Sensitive

区分大小写

  • Strings
  • Variables
  • Object Properties
  • Constants, by default
  • 字符串
  • 变量
  • 对象属性
  • 常量,默认情况下

Case Insensitive

不区分大小写

  • Key Words etc
  • Functions
  • Object Methods
  • Constants, if defined accordingly
  • Class Names
  • 关键词等
  • 职能
  • 对象方法
  • 常量,如果相应定义
  • 类名

Note:

笔记:

  • Classes are thus a mixed bag:
    • The classkeyword is case insensitive
    • Class names are case insensitive, for declaration, instantiation, and static calls
    • Class methods, being functions, are case insensitive
    • Class properties, being variables & constants, are case sensitive
  • Because Strings are case sensitive, anything that relies on strings, such as array keys and values, is also case sensitive
  • 因此,类是一个混合包:
    • class关键字是不区分大小写
    • 类名不区分大小写,用于声明、实例化和静态调用
    • 作为函数的类方法不区分大小写
    • 类属性,即变量和常量,区分大小写
  • 因为字符串区分大小写,任何依赖字符串的东西,比如数组键和值,也是区分大小写的

采纳答案by cmb

Why is PHP partially case senstive?

为什么 PHP 部分区分大小写?

I can only speculate that this stems from very early versions, probably PHP/FI 2.0. The manualexplicitely states:

我只能推测这源于非常早期的版本,可能是 PHP/FI 2.0。该手册明确地指出:

Keep in mind that PHP/FI function names are not case sensitive.

请记住,PHP/FI 函数名称不区分大小写。

Most user input, such as GET and POST parameters, has always been registered as global variables, back then. Treating these as case insensitive would likely have caused issues, and supposedly therefore all variables have been treated as being case sensitive.

当时,大多数用户输入(例如 GET 和 POST 参数)一直被注册为全局变量。将这些视为不区分大小写可能会导致问题,因此据说所有变量都被视为区分大小写。

From what I can tell these have been the only kinds of identifiers in PHP/FI 2.0. All others have been introduced later, apparently mimicking the case-insensitive function names.

据我所知,这些是 PHP/FI 2.0 中唯一的标识符类型。所有其他的都是后来引入的,显然是模仿不区分大小写的函数名称。

Constants, which are special, have only been introduced as of PHP 4 (the PHP 3 manualmentions "constants", but these are nowadays referred to as "literals"). For some mysterious reason (maybe no consensus could be found), it had been decided to allow constant identifiers to be define()d either case sensitive or insensitive on the developers discression. Interestingly, while define()defaults to case sensitive constants, the respective C counterparts (REGISTER_*_CONSTANT) default to case insensitive.

常量是特殊的,从 PHP 4 开始才被引入(PHP 3 手册提到了“常量”,但现在这些被称为“文字”)。出于某种神秘的原因(也许找不到共识),已经决定允许常量标识符define()区分大小写或不区分开发人员的意见。有趣的是,虽然define()默认为区分大小写的常量,但相应的 C 对应项 ( REGISTER_*_CONSTANT) 默认为不区分大小写。

回答by Abdulla Nilam

Case sensitive(both user defined and PHP defined)

区分大小写(用户定义和 PHP 定义)

  • variables
  • constants
  • array keys
  • class properties
  • class constants
  • 变量
  • 常数
  • 数组键
  • 类属性
  • 类常量

Case insensitive(both user defined and PHP defined)

不区分大小写(用户定义和 PHP 定义)

  • functions
  • class constructors
  • class methods
  • keywords and constructs (if, else, null, foreach, echo etc.)
  • 职能
  • 类构造器
  • 类方法
  • 关键字和结构(if、else、null、foreach、echo 等)


In php.net

php.net 中

Basics

基本

Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.

PHP 中的变量由美元符号后跟变量名称表示。变量名区分大小写。

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

变量名遵循与 PHP 中其他标签相同的规则。有效的变量名以字母或下划线开头,后跟任意数量的字母、数字或下划线。作为正则表达式,它可以这样表达:'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'



Some useful Links

一些有用的链接

  1. Userland Naming Guide
  2. Why are functions and methods in PHP case-insensitive?
  3. Are PHP functions case sensitive?
  4. Are PHP keywords case-sensitive?
  5. Is PHP function names case-sensitive or not?
  6. Source of PHP Case Sensitive
  1. 用户空间命名指南
  2. 为什么 PHP 中的函数和方法不区分大小写?
  3. PHP 函数是否区分大小写?
  4. PHP 关键字是否区分大小写?
  5. PHP 函数名是否区分大小写?
  6. PHP 区分大小写的来源

回答by Abhishek Sharma

Case sensitive

区分大小写

variables, constants, array keys, class properties, class constants

变量、常量、数组键、类属性、类常量

Case insensitive

不区分大小写

functions, class constructors, class methods, keywords and constructs (if, else, null, foreach, echo etc.)

函数、类构造函数、类方法、关键字和构造(if、else、null、foreach、echo 等)