Home>Article>Backend Development> How to use function to set variable type in php
Starting from this time, we will start to introduce some more practical or interesting functions. This article will introduce to you how to use functions to set variable types. You can refer to them if necessary.
In PHP, we often need to set a specified type for a variable, but not everyone knows how to set the type of the variable. Today we will learn how to set the type for the variable.
First let’s look at a little chestnut.
The result of this example is
#When we use the settype function, the type of the variable will change, and the variable will be output , the type of the variable has not changed back to its original type.
Then let’s take a closer look at this function.
The settype() function is used to set the type of a variable.
To use the settype() function, you must first pass it a variable to be changed, and a string containing a type in the type list. If the number we need to change was originally a double precision type, and now changes it to an integer or other type, then if the value is set to a double precision type, the lost precision will not be returned.
Therefore, when we set the variable type, we need to set it carefully. Once the setting is wrong, all the original types will not be restored again.
Let’s take a look at the syntax of this function.
bool settype (mixed &$var , string $type)
This$var
means the variable to be converted.
This$type
means the value we may set.
This value includes:
"boolean" (or "bool" since PHP 4.2.0)
"integer" (or "int" since PHP 4.2.0)
"float" (only available since PHP 4.2.0, for older versions "double" used in is now deprecated)
"string"
"array"
"object"
"null" (from PHP 4.2.0)
Of course the return value of this function It is also very important.
This function will returnTRUE
when successfully set, andFALSE
when failed.
That’s all. If you want to know anything else, you can click here. → →php video tutorial
The above is the detailed content of How to use function to set variable type in php. For more information, please follow other related articles on the PHP Chinese website!