Hướng dẫn is php case sensitive

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.

Variables in PHP are represented by a dollar sign followed by the name of the variable. 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 underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: ^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$

Note: For our purposes here, a letter is a-z, A-Z, and the bytes from 128 through 255 [0x80-0xff].

Note: $this is a special variable that can't be assigned. Prior to PHP 7.1.0, indirect assignment [e.g. by using variable variables] was possible.

For information on variable related functions, see the Variable Functions Reference.

By default, variables are always assigned by value. That is to say, when you assign an expression to a variable, the entire value of the original expression is copied into the destination variable. This means, for instance, that after assigning one variable's value to another, changing one of those variables will have no effect on the other. For more information on this kind of assignment, see the chapter on Expressions.

PHP also offers another way to assign values to variables: assign by reference. This means that the new variable simply references [in other words, "becomes an alias for" or "points to"] the original variable. Changes to the new variable affect the original, and vice versa.

To assign by reference, simply prepend an ampersand [&] to the beginning of the variable which is being assigned [the source variable]. For instance, the following code snippet outputs 'My name is Bob' twice:

One important thing to note is that only named variables may be assigned by reference.

It is not necessary to initialize variables in PHP however it is a very good practice. Uninitialized variables have a default value of their type depending on the context in which they are used - booleans default to false, integers and floats default to zero, strings [e.g. used in echo] are set as an empty string and arrays become to an empty array.

Example #1 Default values of uninitialized variables

Relying on the default value of an uninitialized variable is problematic in the case of including one file into another which uses the same variable name. E_NOTICE level error is issued in case of working with uninitialized variables, however not in the case of appending elements to the uninitialized array. isset[] language construct can be used to detect if a variable has been already initialized.

jeff dot phpnet at tanasity dot com

12 years ago

This page should include a note on variable lifecycle:

Before a variable is used, it has no existence. It is unset. It is possible to check if a variable doesn't exist by using isset[]. This returns true provided the variable exists and isn't set to null. With the exception of null, the value a variable holds plays no part in determining whether a variable is set.

Setting an existing variable to null is a way of unsetting a variable. Another way is variables may be destroyed by using the unset[] construct.



is_null[] is an equivalent test to checking that isset[] is false.

The first time that a variable is used in a scope, it's automatically created. After this isset is true. At the point at which it is created it also receives a type according to the context.



If it is used without having been given a value then it is uninitalized and it receives the default value for the type. The default values are the _empty_ values. E.g  Booleans default to FALSE, integers and floats default to zero, strings to the empty string '', arrays to the empty array.

A variable can be tested for emptiness using empty[];



Unset variables are also empty.



Everything above applies to array elements too.

Chủ Đề