In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how php transforms data types. It is very detailed and has a certain reference value. Friends who are interested must finish reading it!
Strong conversion method: 1, add the target type enclosed in parentheses before the variable to be converted ("(int)", "(bool)", "(float)", etc.); 2, use the type conversion function intval (), floatval (), boolval (), strval (), settype ().
Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
PHP forced type conversion
Cast in PHP is similar to other languages in that you can precede the variable to be converted with the target type enclosed in parentheses (for example, $var = (int) 3.1415;), or you can use specific type conversion functions (such as intval (), floatval (), strval (), etc.) or settype () to convert the type.
When you cast a variable type by adding a target type enclosed in parentheses, the types of variables allowed in parentheses are as follows:
(int), (integer): convert to integer
(bool), (boolean): convert to Boolean type
(float), (double), (real): convert to floating point type
(string): convert to string type
(array): converting to an array type
(object): converts to an object type.
When you use specific conversion functions intval (), floatval (), boolval (), strval (), and so on to convert the types of variables, the functions of these functions are as follows:
Intval (): used to get the integer value of a variable
Floatval (): used to get the floating-point value of a variable
Boolval (): used to get the Boolean value of the variable
Strval (): used to get the string value of the variable.
[example] use the above functions to cast the type of a variable.
The running results are as follows:
The type of variable $int is: integer variable $float is of type: double variable $string is of type: string
Neither of the two cast methods described above changes the type of the converted variable itself, but by assigning the new type of data converted to the new variable, the type and value of the original variable remain unchanged.
If you need to change the type of the variable itself, you can use the settype () function, which has the following syntax format:
Settype (mixed & $var, string $type)
Where $var is the variable to be converted; $type is the type to be converted, which can be boolean (bool), integer (int), float (double), string, array, object, null.
[example] use the settype () function to cast the type of variable.
The running results are as follows:
The type of $str is: type of integer$bool is: string
There are the following points to note when using cast:
Integer type is converted to floating point type, and because the precision range of floating point type is much larger than that of integer type, the precision after conversion will not change.
When a floating-point type is converted to an integer, the fractional part is automatically discarded, leaving only the integer part. If a floating point number exceeds the valid range of an integer number, the result will be uncertain.
When a string is converted to a number, it starts from the string to the end of the first character that is not a number (the content that does not match is cleared), that is, if the first character of the string is not a number, the converted result is 0
The NULL value is converted to a string and the result is an empty character.
The above is all the content of the article "how php transforms data types". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.