Through the previous series of learning to understand the PHP constants and variables, then you know what the PHP constants and variables are related and different? Don't worry, just take a look here. The relationship and difference between PHP constants and variables
Let's look at what variables and constants are .
During program execution, the value stored by the variable can be changed at any time, and the value stored in the constant cannot be changed.
Variables are used to store temporary data information. When a variable is defined, the system automatically assigns the variable a storage space value. We can assign a value to a variable when it is defined, and if you need to change the value of the variable, simply
can be assigned. For those temporary data information or process, can be stored in variables.
Constants are used to store data information that is infrequently changed. You can assign a value when you define a constant, which is valid during the entire execution period of the program and cannot be assigned to the constant again.
Let's start with the definition of PHP constants and variables to see how they differ:
Constants: In PHP We use the Define () function to define constants see: Definition of PHP constants and usage examples
Variables: All variables in PHP are preceded by $, followed by the name of the variable, assigning a value to the variable. See: Definitions and usage examples of PHP variables
From the definition of PHP constants and variables, we can get:
The constant is preceded by a dollar sign ($);
Constants can only be defined with the Define () function, not through assignment statements;
Take a look at the difference between the PHP constants and the variables defined :
Constants: Constants cannot be redefined or undefined once they are defined.
Variables: Variables refer to the amount of values that can change during the execution of a program, as opposed to the constants we speak.
So we can get the results again:
Constants cannot be changed as long as they are defined, and variables can be changed.
Then look at the values of constants and variables :
Constants: The value of a constant can only be scalar, that is, an integer floating-point Boolean null string, etc., cannot be an array object resource, and the variable can be all PHP data types (PHP eight data types).
Look at the scope of the PHP constants and variables:
The scope of a constant: constants can be called directly, regardless of the definition, without scope.
Scope of the variable: The variable definition is valid in that range, as the variable defined in the function is only valid in the function.
Summarize:
Through the above we can summarize the difference between the PHP constants and the variables above:
1. Constant preceded by no dollar sign ($)
2. Constants can only be defined with the Define () function, but not by an assignment statement
3. Constants may be defined and accessed anywhere
4 without regard to the rules of variable scope. Constants cannot be redefined or undefined once defined br> 5. The value of a constant can only be a scalar