In PHP, you can use type conversion to convert a variable or value to another type. This type of conversion is the same as that in C language. You only need to insert the temporary data type to be converted in parentheses before the variable to be converted.
1. type conversion
In PHP, you can use type conversion to convert a variable or value to another type. This type of conversion is the same as that in C language. You only need to insert the temporary data type to be converted in parentheses before the variable to be converted.
For example, we can declare two variables in the previous article using type conversion.
$ Totalqty = 0;
$ Totalamount = (float) $ totalqty;
The 2nd line of code refers to "retrieving the value of the variable saved in $ totalqty, interpreting it as a floating point type, and saving it in $ totalamount. The $ totalamount variable changes to the floating point type. The converted variable does not change its type, so $ totalqty is still an integer.
Implicit or explicit type conversion may occur.
2. variable
PHP provides a variable of another type-variable. Variable variables allow us to dynamically change the name of a variable. We can see that PHP has a great degree of freedom in this regard-all languages allow changing the value of a variable, but not many languages allow changing the type of a variable, as for languages that support variable name change, there will be fewer languages. This feature uses the value of one variable as the name of another variable. For example, we can set:
$ Username = 'feng ';
Therefore, we can replace $ feng with $ username. For example, we can set the value of $ feng:
$ Username = 100;
This is equivalent:
$ Feng = 100;
This code does not seem easy to understand. I will introduce it in detail in later articles.