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
5
  • 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. Commented Jun 22, 2018 at 4:42
  • 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. Commented Dec 30, 2018 at 2:37
  • 5
    I would like to add that namespaces are also Case Insensitive.
    – Alexandru
    Commented Jul 17, 2019 at 7:14
  • 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.
    – Arno Hilke
    Commented Jul 25, 2019 at 9:01
  • 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.
    – Christian
    Commented Jul 11 at 1:44

3 Answers 3

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
8
  • 7
    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?
    – Manngo
    Commented Feb 21, 2018 at 9:06
  • 2
    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...
    – McVitas
    Commented Mar 1, 2018 at 20:22
  • 2
    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.
    – Codesmith
    Commented Mar 21, 2018 at 16:11
  • Can I ask why you had this question protected?
    – Manngo
    Commented Dec 30, 2018 at 2:16
  • @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.
    – Manngo
    Commented Dec 30, 2018 at 2:21
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.

2
  • You know, I never noticed the extra optional parameter on the define function.
    – Manngo
    Commented Oct 22, 2017 at 1:39
  • 1
    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.
    – Manngo
    Commented Dec 30, 2018 at 2:40
-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.)

1
  • 14
    Yes, but why?
    – Manngo
    Commented Oct 22, 2015 at 5:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.