Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP & Case Sensitivity [duplicate]

In PHP, variable and constant names are case sensitive, while function 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
like image 594
Manngo Avatar asked Oct 22 '15 05:10

Manngo


People also ask

What PHP stand for?

PHP, originally derived from Personal Home Page Tools, now stands for PHP: Hypertext Preprocessor, which the PHP FAQ describes as a "recursive acronym." PHP executes on the server, while a comparable alternative, JavaScript, executes on the client.

Is PHP used anymore?

PHP is known to be the most frequently used programming language. According to W3Techs, 78.8% of all websites are using PHP for their server-side. Interesting fact: PHP originally stood for Personal Home Page. Now PHP is widely known and thought of as Hypertext Preprocessor.

What is PHP?

What is PHP? PHP is a script on the server-side used for the creation of Static or Dynamic Web sites or Web applications. PHP is a pre-processor for hypertext, which used to stand for home pages. The software used to build web applications is an open-source, server-side scripting language.

What is the use of PHP operators?

Operators are used to perform operations on variables and values. The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc. The PHP assignment operators are used with numeric values to write a value to a variable.

What is the difference between&&and and and in PHP?

In addition to using the and keyword, PHP uses && as the logical AND operator: The && and and operators return the same result. The only difference between the && and and operators are their precedences.

Is there a free interactive PHP tutorial?

Welcome to the learn-php.org free interactive PHP tutorial. Whether you are an experienced programmer or not, this website is intended for everyone who wishes to learn the PHP programming language. There is no need to download anything - just click on the chapter you wish to begin from, and follow the instructions.


2 Answers

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
like image 102
Abdulla Nilam Avatar answered Oct 25 '22 11:10

Abdulla Nilam


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.

like image 21
cmb Avatar answered Oct 25 '22 13:10

cmb