Home > Article > Backend Development > New to PHP, learn variables and constants_PHP Tutorial
In a process of change, the quantity that remains unchanged is called constant, while the variable is The variable is a continuous storage space with a name. The following is the basic knowledge of PHP variables and constants.
For introductory learning of PHP variables, this introductory tutorial is divided into the following parts: how to identify PHP variables, how to declare PHP variables, how to assign values to PHP variables, introduction to the types of PHP variables, and introduction to commonly used PHP variable functions.
For introductory learning of PHP constants, this article mainly introduces the definition and usage of PHP constants.
1. How to identify PHP variables
The so-called identifier is actually the PHP variable name, which mainly consists of letters, numbers, underscores and dollar signs ($). The length can be arbitrarily long and cannot start with a number. Remember that in PHP, variables are case-sensitive (the exception is the function that comes with the PHP system, which is not case-sensitive).
Special reminder: When defining PHP variables, it is best not to use the same name as the PHP system's built-in functions or built-in system variables. It is easy to get confused. In addition, when defining PHP variables, in order to maintain better coding As a habit, when a variable consists of multiple words, the first letter of the first word should be lowercase, the second word should have a capital letter..., and so on. This is my personal suggestion.
2. PHP variable declaration and assignment
Unlike programming languages such as C++, PHP does not need to declare it in advance when using variables. It's ready to use, use '=' when assigning values. Such as
<ol class="dp-c"><li class="alt"><span><span class="vars">$test</span><span> = </span><span class="string">'欢迎访问www.leapsoul.cn,这里有最新的PHP入门教程'</span><span>; </span></span></li></ol>
3. PHP variable types
Like other languages, PHP variable types also support integers, strings, arrays, objects, etc. The difference is that in other languages, such as C language, the data type of the variable needs to be declared in advance before using the variable, while the data type of the PHP variable does not need to be declared in advance and is determined when you assign it a value. For example,
<ol class="dp-c"> <li class="alt"><span><span class="vars">$leapsoul</span><span> = 1; </span><span class="comment">//定义PHP变量的数据类型为整型 </span><span> </span></span></li> <li> <span class="vars">$leapsoul</span><span> = </span><span class="keyword">array</span><span>();</span><span class="comment">//定义PHP变量的数据类型为数组 </span><span> </span> </li> <li class="alt"> <span class="vars">$leapsoul</span><span> = </span><span class="string">"欢迎访问www.leapsoul.cn,本文主要介绍PHP变量与常量的学习"</span><span>;</span><span class="comment">//定义PHP变量的数据类型为字符串</span><span> </span> </li> </ol>
There is a special data type in PHP - indefinite variable, which allows us to dynamically modify the variable name. Before, we mentioned that the definition of PHP variables starts with a dollar sign ($). If Add a dollar sign ($) at the beginning and it becomes an indefinite variable, that is,
<ol class="dp-c"> <li class="alt"><span><span class="vars">$test</span><span> = </span><span class="string">'leapsoul'</span><span>; </span></span></li> <li> <span>$</span><span class="vars">$test</span><span> = </span><span class="string">"欢迎访问www.leapsoul.cn,本文主要介绍PHP变量与常量的学习"</span><span>; </span> </li> </ol>
is equivalent to
<ol class="dp-c"><li class="alt"><span><span class="vars">$leapsoul</span><span> = </span><span class="string">"欢迎访问www.leapsoul.cn,本文主要介绍PHP变量与常量的学习"</span><span>; </span></span></li></ol>
4. PHP variable function
PHP variable function is mainly used to judge the data type of variables and the existence of variables. The functions for testing PHP variable types are :
gettype(): Returns the data type of the passed variable. If it is not a standard data type, such as integer, string, array, object, etc., it returns unknown type;
settype (): Change the data type of the passed variable, similar to forced type conversion.
<ol class="dp-c"> <li class="alt"><span><span class="vars">$test</span><span>; </span></span></li> <li> <span class="vars">$leapsoul</span><span> = </span><span class="string">"PHP入门教程之PHP变量与常量学习"</span><span>; </span> </li> <li class="alt"> <span class="func">echo</span><span> </span><span class="func">gettype</span><span>(</span><span class="vars">$test</span><span>);</span><span class="comment">//输出NULL </span><span> </span> </li> <li> <span class="func">echo</span><span> </span><span class="func">gettype</span><span>(</span><span class="vars">$leapsoul</span><span>);</span><span class="comment">//输出变量类型为string </span><span> </span> </li> <li class="alt"> <span>settype(</span><span class="vars">$leapsoul</span><span>,</span><span class="string">"int"</span><span>);</span><span class="comment">//设定$leapsoul变量类型为int </span><span> </span> </li> <li> <span class="func">echo</span><span> </span><span class="func">gettype</span><span>(</span><span class="vars">$leapsoul</span><span>);</span><span class="comment">//数据变量类型为integer</span><span> </span> </li> </ol>
The function to determine whether a PHP variable is a specific data type is
is_array(): Determine PHP Whether the variable type is an array type
is_string(): Determine whether the PHP variable type is a string type
is_object(): Determine whether the PHP variable type is an object type
More For similar functions, you can refer to PHP’s help documentation
Function to test the existence of PHP variables
Mainly uses the isset and empty functions. The difference is isset The function is used to determine whether the variable exists. If it exists, it returns true, otherwise it returns false. The empty function is mainly used to determine whether the value of this variable is empty, or whether the variable has a value assigned. If it is empty, it returns true, otherwise Return false. These two functions are very useful when PHP form variables are submitted to background processing. In principle, isset is first used to judge the existence of the variable. If the variable exists, the empty function can be used for the variable value of the required option as needed. to make a judgment.
This completes the introduction to the relevant knowledge of PHP variables in the PHP introductory tutorial. Let’s take a look at how to use and define PHP constants
How to define and use PHP constants
PHP constants are defined through the define function. Constant names generally use uppercase letters. Once a constant is defined, it cannot be changed during the script process. Usually when developing large projects, we generally use some commonly used functions. The constants are placed in a configuration file in advance and included when used, which also facilitates management.
<ol class="dp-c"> <li class="alt"><span><span>define(</span><span class="string">"INTRO"</span><span>,</span><span class="string">"这段代码展示了PHP常量该如何定义与使用"</span><span>); </span></span></li> <li> <span class="func">echo</span><span> INTRO; </span> </li> </ol>
Through the above code example, we can see that the difference between PHP constants and variables is that when using a constant, there is no dollar sign in front of it, only Just use its name, and variables are used with dollar signs.
You can see more PHP system constants and environment variables through the phpinfo() function. Detailed description of PHP environment variables $_SERVER and system constants
At this point, the basic knowledge of PHP variables and constants in the basic syntax of the PHP introductory tutorial has been introduced. In the next PHP introductory tutorial, I will mainly introduce PHP The definition and use of functions, and also introduces the relevant knowledge of PHP variable scope.
Original address: http://www.leapsoul.cn/?p=427