Home >Backend Development >PHP Problem >How to define variables in php
Variables refer to quantities that can change at any time during the running of the program. You can use only one variable or multiple variables in the code. Words, words, etc. can be stored in the variables. Values, dates, attributes, etc. The value of a variable is temporary. It exists when the program is running. If the program ends, the value of the variable will be lost. Although variables are also used in the previous examples, there are no detailed instructions. This section will introduce in detail how to create variables and how to reference variables.
Create variables
Variables in PHP are identifiers prefixed with a dollar sign ($). An identifier is a symbol that identifies different objects, such as the name of a variable, a function name, or the name of other user-defined objects. In PHP, the naming of identifiers must comply with the following regulations:
Identifiers can consist of one or more characters, but must begin with a letter or an underscore. Additionally, identifiers may only consist of letters, numbers, underscore characters, and other ASCII characters from 127 to 255. Identifier names such as my_a, Ss, and _value are all legal, but variable names such as q^a and 4tt are illegal.
Identifiers are case-sensitive. Therefore, the variable $recipe is different from the variables $Recipe, $rEciPe or $recipE.
Identifiers can be of any length. This is beneficial because it allows the programmer to accurately describe the purpose of the identifier through its name.
The identifier name cannot be the same as any PHP predefined keyword.
In the process of creating variables, it is a good habit to declare the variable first and then assign a value to the variable. Since PHP is a weakly typed language, when declaring a variable, there is no need to explicitly declare the variable. The variable can store any type of value. In PHP, the variable is type checked at runtime and can be replaced by another value of a different type. The value of the variable, declare a variable below, and let another value of a different type replace the value of the variable, and then declare a variable without assignment. The specific code is as follows:
$what = “Yound Tang”; $what = 25; $name;
In PHP, give the variable There are two ways of assignment, namely value assignment and reference assignment. Value assignment is to directly copy a value to a variable through an assignment expression, which will overwrite the original value of the variable. If there is no assignment when declaring the variable, its behavior will be the same as NULL. Assigning a value when declaring a variable is a commonly used variable assignment method. The usage example is as follows:
$name = “唐晓阳”; $age = “23”; $sex = “男”; echo “你的姓名是:”.$name.”<br/>”; echo “你的年龄是:”.$age.”<br/>”; echo “你的性别是:”.$sex.”<br/>”;
Execute this code and the execution result is as follows:
你的姓名是:唐晓阳 你的年龄是:23 你的性别是:男
In PHP, You can declare a variable to assign a value directly or not. When you need to use a variable to store a value, you can assign a value by reference. Assignment by reference means that the created variable has the same content as that referenced by another variable. Therefore, if multiple variables refer to the same content, modifying any one of them will be reflected in the remaining variables. Reference assignment can be completed by adding an & symbol after the equal sign. An example form of reference assignment is shown below.
$value1="Hello World"; value2=&value1; $value2="GoodBye"; echo $value1.”<br/>”; echo $value2.”<br/>”;
In the above code, a variable value1 is created and assigned the value "Hello World". In the following statement, the variable $value2 uses reference assignment, that is, the value of value1 is assigned to value2. At this time These two variables are a life community. When one changes, the other will display the result. The execution result of this code is as follows:
GoodBye GoodBye
The above is the detailed content of How to define variables in php. For more information, please follow other related articles on the PHP Chinese website!