Home >Backend Development >PHP Tutorial >PHP naming case sensitivity rules
This article introduces the case-sensitive rules of PHP naming. It has certain reference value. Now I share it with you. Friends in need can refer to it
I have always felt that the various capitalization rules in PHP are unclear. Even veterans who have worked for many years may not have enough understanding of PHP's capitalization sensitivity issues. In PHP, the handling of case-sensitive issues is messy, so everyone must pay attention. Even if some places are not case sensitive, it is best to always adhere to "case sensitivity" during the programming process. Here are some notes on case issues:
Case Sensitive
1. Variable names are case sensitive
All variables are case sensitive, including ordinary variables and $_GET,$_POST,$_REQUEST,$ _COOKIE,$_SESSION,$GLOBALS,$_SERVER,$_FILES,$_ENV, etc.;
<?php $abc = 'abc'; echo $abc; //输出'abc' echo $aBc; //无输出 echo $ABC; //无输出 ?>
2. Constant names are case-sensitive
Constants defined using define are case-sensitive.
<?php define('BLOGGER','Veitor'); echo BLOGGER; //输出'Veitor' echo BLOgger; //报NOTICE提示,并输出'BLOgger' echo blogger; //报NOTICE提示,并输出'blogger' ?>
3. Array index (key name) is case-sensitive
<?php $arr = array('one'=>'first'); echo $arr['one']; //输出'first' echo $arr['One']; //无输出并报错 echo $Arr['one']; //上面讲过,变量名区分大小写,所以无输出并报错 ?>
Case-insensitive
1. Function Names, method names, and class names are not case-sensitive
Although these are not case-sensitive, we adhere to the "case-sensitive" principle and it is recommended to use names with the same case as when defined
Copy code
<?php class Test { static public function Ceshi() { echo '123'; } public funcion Dxx() { echo '321'; } } $obj = new Test; $obj->Dxx(); //成功实例化Test类,并调用Dxx方法输出'321' $obj->dxx(); //成功实例化Test类,并调用Dxx方法输出'321' $obj = new test; $obj->Dxx(); //成功实例化Test类,并调用Dxx方法输出'321' $obj->dxx(); //成功实例化Test类,并调用Dxx方法输出'321' Test::Ceshi(); //输出'123' test::Ceshi(); //输出'123' Test::ceshi(); //输出'123' test::ceshi(); //输出'123' ?>
2. Magic constants are not case-sensitive
Some magic constants include: __LINE__, __FILE__, __DIR__, __FUNCTION__, __CLASS__, __METHOD__, __NAMESPACE__, etc. are not case-sensitive.
<?php echo __LINE__; //输出2 echo __line__; //输出3 ?>
3. NULL, TRUE, and FALSE are not case-sensitive
Many people probably know this, so I won’t give an example.
4. Forced type conversion is not case-sensitive
Such as these
(int), (integer) - converted to integer type
(bool), (boolean) - converted to Boolean type
(float), (double), (real) – Convert to floating point type
(string) – Convert to string
(array) – Convert to array
(object) – Convert to object
Generally we all lowercase, this is not a big problem.
In general, the things that are easy to be confused about are variables, constants, class names, method names and function names. It will be helpful for you to remember these.
Related recommendations:
Introduction to php namespace
PHP Detailed explanation of namespace and automatic loading instances
The above is the detailed content of PHP naming case sensitivity rules. For more information, please follow other related articles on the PHP Chinese website!