php method to convert numbers into strings: 1. Add the target type enclosed in parentheses before the variable to be converted, for example "(string)3.14"; 2. Use the strval() function, For example "strval(3.14)"; 3. Use the settype() function.
Recommended: "PHP Video Tutorial"
PHP data type conversion (character conversion Numbers, numbers to characters)
PHP data type conversion is forced conversion. The PHP data types that are allowed to be converted are:
(int) , (integer): converted to integer
The first conversion method: (int) (bool) (float) (string) (array) (object)
<?php $num1=3.14; $num2=(string)$num1; var_dump($num1); //输出float(3.14) var_dump($num2); //输出string(3.14) ?>
float 3.14 string '3.14' (length=4)
The second conversion Method: intval() floatval() strval()
<?php $str="123.9abc"; $int=intval($str); //转换后数值:123 $float=floatval($str); //转换后数值:123.9 $str=strval($float); //转换后字符串:"123.9" var_dump($int); //输出int(123) var_dump($float); //输出float(123.9) var_dump($str); //输出string(123.9) ?>
int 123 float 123.9 string '123.9' (length=5)
The third conversion method: settype()
<?php $num4=12.8; $flg=settype($num4,"string"); var_dump($flg); //输出bool(true) var_dump($num4); //输出string(12.8) ?>
boolean true string '12.8' (length=4)
The settype() function is used to set the type of a variable.
PHP version requirements: PHP 4, PHP 5, PHP 7Syntaxbool settype ( mixed &$var , string $type )
If you want to get more relevant knowledge, you can visit:
The above is the detailed content of How to convert numbers to string in php?. For more information, please follow other related articles on the PHP Chinese website!