Home >Backend Development >PHP Problem >How to convert integer to string type in php?
Methods to convert integers to string types in php: 1. Use forced type conversion and add the target type "(string)" enclosed in parentheses before the integer variable to be converted; 2 , use the strval() function; 3. Use the "settype (integer variable, "string")" method.
Recommended: "PHP Video Tutorial"
Method 1: Forced type conversion---in The variable to be converted is preceded by the target type "(string)" enclosed in parentheses
Example:
<?php $num1=365765; $num2=(string)$num1; var_dump($num1); //输出int(3.14) var_dump($num2); //输出string(3) ?>
Output result:
Method 2: Use strval() function
strval(): used to get the string value of a variable.
Example
<?php $int=545248; $str=strval($int); var_dump($str); ?>
Output:
string '545248' (length=6)
Method 3: Use settype(mixed var,string type)
The settype() function is used to set the type of a variable.
Syntax
bool settype ( mixed &$var , string $type )
Parameter description:
$var: The variable to be converted.
$type: Possible values for type are.
"boolean" (or "bool" since PHP 4.2.0)
"integer" (or "int" , starting from PHP 4.2.0)
"float" (only available after PHP 4.2.0, "double" used in older versions is now deprecated)
"string"
"array"
"object"
"null" (since PHP 4.2.0)
Example:
<?php $int=545248; $str=settype($int,"string"); var_dump($str); //输出bool(true) var_dump($int); //输出string '545248' ?>
Output:
boolean true string '545248' (length=6)
The above is the detailed content of How to convert integer to string type in php?. For more information, please follow other related articles on the PHP Chinese website!