Home >Backend Development >PHP Problem >How to convert numbers to string type in php
php method to convert numbers to strings: 1. Add the target type "(string)" enclosed in parentheses before the numeric variable to be converted, the syntax is "(string)$num"; 2 , use strval() function, syntax "strval($num)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php converts numbers For string type
#Method 1: Add the target type enclosed in parentheses "(string)
"# before the numeric variable to be converted.
( string): It is forced to convert to string type;
<?php header("Content-type:text/html;charset=utf-8"); $num = 12354; echo '原变量类型为:' . gettype($num) . '<br>'; $str = (string)$num; echo '转换后的变量类型为:' . gettype($str) . '<br><br>'; $num = 123.54; echo '原变量类型为:' . gettype($num) . '<br>'; $str = (string)$num; echo '转换后的变量类型为:' . gettype($str) . '<br><br>'; ?>##Method 2: Use strval() function
strval() function is used to get the string value of a variable.
<?php header("Content-type:text/html;charset=utf-8"); $num = 3.1415; echo '原变量类型为:' . gettype($num) . '<br>'; $str = strval($num); echo '转换后的变量类型为:' . gettype($str) . '<br><br>'; $num = 31415; echo '原变量类型为:' . gettype($num) . '<br>'; $str = strval($num); echo '转换后的变量类型为:' . gettype($str) . '<br><br>'; ?>
Recommended learning: "
PHP Video TutorialThe above is the detailed content of How to convert numbers to string type in php. For more information, please follow other related articles on the PHP Chinese website!