Home > Article > Backend Development > A brief discussion on the 8 types of PHP variables and weak types
In this article, the editor will talk to you about the 8 types of PHP variables and weak types. Interested friends can learn more.
1, 8 types of PHP variables
Four scalar types:
Two composite types:
Finally there are two special types:
Second, PHP weak type
We know that PHP is a It is a weakly typed language (when initializing a variable, you do not need to specify its type), but the previous 8 types of PHP variables are provided. These two sentences seem a bit contradictory. How do we understand the types of PHP?
First of all, there are 8 types at the bottom of PHP.
Secondly, when you initialize or assign a value to a variable, the bottom layer of PHP will automatically identify the value that is about to be assigned, and then set the type of the variable.
# Again, when you make a logical judgment on two variables, the bottom layer of PHP will perform type conversion according to the following rules and then make a logical judgment.
#1, the logical judgment is an arithmetic operator, addition, subtraction, multiplication (-*), if any operand is a floating point number, then all operands are Treated as a floating point number, the result is also a floating point number. Otherwise the operand will be interpreted as an integer, and the result will also be an integer,
2. The logical judgment is the arithmetic operator, division (/), the division operator is always Returns a floating point number. The only exception is if both operands are integers (or integers converted from strings) and are exactly divisible, in which case it returns an integer.
#3, logical judgment is an arithmetic operator, modulo (%), all operation bits are converted into integers, and the result is also an integer.
#4, the logical judgment is an arithmetic operator, negation (-), if the operand is a floating point number, the result is also a floating point number, otherwise it is always first Convert the operand to an integer, and the result is also an integer.
#5, logical judgment is a bit operator, all operands are converted into integers, and the result is also an integer.
#6, the logical judgment is a comparison operator. If one of the operands is a Boolean value, the other operation fee will be converted to a Boolean value. If one If the operand is a floating-point type, the other operand will also be converted to a floating-point type. In other cases, both operands will be converted to integers. Regardless of the type of the operands, the result of a comparison operator is always a Boolean value.
Related tutorials: PHP video tutorial
The above is the detailed content of A brief discussion on the 8 types of PHP variables and weak types. For more information, please follow other related articles on the PHP Chinese website!