Case sensitivity in PHP
Summary
PHP exhibits a mixed approach to case sensitivity, which can be inconsistent across different language constructs. Developers should note that variable names, including superglobals, and constant names are case-sensitive by default, as are php.ini configurations. Conversely, function, method, and class names are case-insensitive, though maintaining consistent casing is strongly advised for readability. Magic constants, the boolean values NULL, TRUE, FALSE, and type casting keywords also operate without case sensitivity. Adhering to a generally case-sensitive coding style is recommended to mitigate potential issues and improve code clarity.
1. Case sensitive
1.1 Variable name is case sensitive
All variables names are case sensitive, these include normal variables and superglobals such as $_GET,$_POST,$_REQUEST,$_COOKIE,$_SESSION,$_GLOBALS etc.
<?php
$abc = 'abcd';
echo $abc; //Output 'abcd'
echo $aBc; //No output
echo $ABC; //No output
1.2 Constant name by default is case sensitive, usually use UPPER CASE for constant name
<?php
define("ABC","Hello World");
echo ABC; //Output Hello World
echo abc; //Output abc
1.3 Configuration in php.ini
For example, file_uploads=1 cannot be written as File_Uploads = 1
2. Case insensitive
2.1 Function name, method name or class name are case insensitive, but we recommend that you use the same name as you define them.
Function name
<?php
function show(){
echo "Hello World";
}
show(); //Output Hello World
SHOW(); //Output Hello World
Method name and class name
<?php
class cls{
static function func(){
echo "hello world";
}
}
Cls::FunC(); //Output hello world
2.2. Magic constants are case insensitive
Include __LINE__,__FILE__,__DIR__,__FUNCTION__,__CLASS__,__METHOD__
<?php
echo __line__; //Output 2
echo __LINE__; //Output 3
2.3 NULL, TRUE, FALSE are case insensitive
<?php
$a = null;
$b = NULL;
$c = true;
$d = TRUE;
$e = false;
$f = FALSE;
var_dump($a == $b); //Output boolean true
var_dump($c == $d); //Output boolean true
var_dump($e == $f); //Output boolean true
2.4 Type casting
(int),(integer) – Convert to integer
(bool),(boolean) – convert to boolean
(float),(double),(real) – convert to floating point
(string) – convert to string
(array) – convert to array
(object) – convert to object
Source : http://hi.baidu.com/chendeping/blog/item/2239b72a708baf96033bf685.html
No comment for this article.