PHP & Case Sensitivity [duplicate]
Asked Answered
L

3

61

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

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.

Why is PHP partially case senstive?

Please note, that I am not asking which names 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 class keyword 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
Lakieshalakin answered 22/10, 2015 at 5:27 Comment(5)
I don't know why, maybe they designed the language that way. But it is a good practice to consider PHP as case-sensitive and use the functions/variables as declared.Alienation
Noticing the edit and visiting the question, am wondering why it's still considered as unsolved? Edit: I think this would probably considered as being too broad. Probably nobody but the people at PHP.net would be able to answer this completely or near as they can.Guitar
I would like to add that namespaces are also Case Insensitive.Vestavestal
Also worth noting is that type hints are case insensitive. For classes this is less surprising, but it also applies to primitive types like bool. Thankfully those are forbidden to use as class names and thus cannot clash.Oshea
Small hint as I mentioned below: autoloading might be case sensitive (depending on the autoloader). E.g. if MyClass has already been loaded, MyClass::XX and myclass::XX will both work. If the autoloader is case sensitive, the class has not been loaded yet and you try to use myclass::XX, it would fail.Exarate
S
27

Why is PHP partially case senstive?

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

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

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.

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.

Constants, which are special, have only been introduced as of PHP 4 (the PHP 3 manual mentions "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.

Segarra answered 17/9, 2017 at 16:7 Comment(2)
You know, I never noticed the extra optional parameter on the define function.Lakieshalakin
I’m accepting this answer since it seems that there really isn’t a good reason. It appears to be a legacy of a mixed heritage.Lakieshalakin
V
39

FYK (updated)


Case sensitive (both user-defined and PHP defined)

  • variables
  • constants ->>check Amendment 1
  • array keys
  • class properties
  • class constants

Case insensitive (both user defined and PHP defined)

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

In php.net

Basics

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

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscores, 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]*'


Amendments

  1. Class constants are always case-sensitive. Global constants declared with const are always case-sensitive. It should be noted that this applies only to the shortname of the constant, while namespaces in PHP are always case-insensitive. Constants declared with define() are case-sensitive by default

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. Are PHP function names case-sensitive or not?
  6. Source of PHP Case Sensitive
Vivienne answered 22/10, 2015 at 5:29 Comment(8)
Sorry, but I just have to comment that this in no way answers the question. I didn’t ask how to create variables, or which names are case sensitive. I asked why?Lakieshalakin
I wonder why there is always somebody who promptly copy pastes part of some manual without actually reading the whole OP... :-\ This is not an answer at all! My answer would be that this is just because PHP was made by more developers and they simply did not communicate properly, thus causing this confusing mess. My question is how can I disable this weird behavior? I find case sensitivity in variable names just as a huge opportunity for various hard to find bugs and errors. There is no other value in this approach sorry...Photoneutron
Keep in mind that TRUE and FALSE are also case-insensitive as they're also language constructs, and not constants in a technical sense, since multiple values can be equal to them. e.g., 1==TRUE, "xyz"==TRUE, etc.Maintenon
Can I ask why you had this question protected?Lakieshalakin
@AbdullaNilam Already read that. In particular, the part with Questions are usually protected because they have attracted either spam answers or "noisy" answers such as "thank you", "this worked for me", or "I'm also having this problem" from new users who may mistake the site as a traditional forum. The only problem here is not that type of response, but answers which ignore the question entirely.Lakieshalakin
@AbdullaNilam Sorry, but that includes your answer, See my first comment to your answer.Lakieshalakin
+ goto labels are case sensitiveDoughy
"class constructors" is a normal "class method"... what you really meant here is the class name itself. MyClass::xx and myclass::xx refer to the same thing when such a class has been loaded - autoloading might be case sensitive though. example: 3v4l.org/NrsZMExarate
S
27

Why is PHP partially case senstive?

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

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

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.

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.

Constants, which are special, have only been introduced as of PHP 4 (the PHP 3 manual mentions "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.

Segarra answered 17/9, 2017 at 16:7 Comment(2)
You know, I never noticed the extra optional parameter on the define function.Lakieshalakin
I’m accepting this answer since it seems that there really isn’t a good reason. It appears to be a legacy of a mixed heritage.Lakieshalakin
P
-1

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.)

Palatable answered 22/10, 2015 at 5:29 Comment(1)
Yes, but why?Lakieshalakin

© 2022 - 2024 — McMap. All rights reserved.