Conversion method: 1. Use strval(), syntax "strval(value)"; 2. Use settype(), syntax "settype (value, "string")"; 3. Use sprintf() function , the syntax is "sprintf(value)"; 4. Use the number_format() function.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php transfers the value Into a string
#Method 1: Use strval() function
strval(): Used to obtain the string value of a variable.
<?php header("Content-type:text/html;charset=utf-8"); var_dump(strval(TRUE)); var_dump(strval(FALSE)); var_dump(strval(12.35)); var_dump(strval(null)); ?>
Method 2: Use the settype() function
settype ($var,$type)
The function is used to set the variable $var
to the specified data type $type
.
<?php header("Content-type:text/html;charset=utf-8"); $num=12.63; $bool1=TRUE; $bool2=FALSE; settype ($num,"string"); settype ($bool1,"string"); settype ($bool2,"string"); var_dump($num); var_dump($bool1); var_dump($bool2); ?>
Method 3: Use sprintf() function
sprintf() function writes the formatted string into a variable middle.
<?php header("Content-type:text/html;charset=utf-8"); var_dump(sprintf(TRUE)); var_dump(sprintf(FALSE)); var_dump(sprintf(null)); var_dump(sprintf(12)); var_dump(sprintf("%.1f",12.35)); var_dump(sprintf("%.3f",12.35)); ?>
Method 4: Use the number_format() function
number_format() can be used to format numbers by thousands grouping.
<?php header("Content-type:text/html;charset=utf-8"); var_dump(number_format(TRUE)); var_dump(number_format(FALSE)); var_dump(number_format(null)); var_dump(number_format(12)); var_dump(number_format(12.35,1)); var_dump(number_format(12.35,3)); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert value into string in php. For more information, please follow other related articles on the PHP Chinese website!