PHP naming case sensitivity rules

不言
Release: 2023-03-24 07:24:01
Original
2426 people have browsed it

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 = &#39;abc&#39;;
echo $abc;    //输出&#39;abc&#39;
echo $aBc;    //无输出
echo $ABC;    //无输出
?>
Copy after login

2. Constant names are case-sensitive

Constants defined using define are case-sensitive.

<?php
define(&#39;BLOGGER&#39;,&#39;Veitor&#39;);
echo BLOGGER;    //输出&#39;Veitor&#39;
echo BLOgger;    //报NOTICE提示,并输出&#39;BLOgger&#39;
echo blogger;    //报NOTICE提示,并输出&#39;blogger&#39;
?>
Copy after login

3. Array index (key name) is case-sensitive

<?php
$arr = array(&#39;one&#39;=>&#39;first&#39;);
echo $arr[&#39;one&#39;];    //输出&#39;first&#39;
echo $arr[&#39;One&#39;];    //无输出并报错
echo $Arr[&#39;one&#39;];    //上面讲过,变量名区分大小写,所以无输出并报错
?>
Copy after login

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 &#39;123&#39;;
    }
    public funcion Dxx()
    {
        echo &#39;321&#39;;
    }
}
$obj = new Test;
$obj->Dxx();    //成功实例化Test类,并调用Dxx方法输出&#39;321&#39;
$obj->dxx();    //成功实例化Test类,并调用Dxx方法输出&#39;321&#39;
$obj = new test;
$obj->Dxx();    //成功实例化Test类,并调用Dxx方法输出&#39;321&#39;
$obj->dxx();    //成功实例化Test类,并调用Dxx方法输出&#39;321&#39;
Test::Ceshi();    //输出&#39;123&#39;
test::Ceshi();    //输出&#39;123&#39;
Test::ceshi();    //输出&#39;123&#39;
test::ceshi();    //输出&#39;123&#39;
?>
Copy after login

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
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!