Home >Backend Development >PHP Problem >How to convert integer to string in php
How to convert an integer to a string 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, the syntax format is "strval (integer variable)", and the return value is a string type.
The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer
php will convert the integer to String method
#1. Forced type conversion--Add the target type enclosed in parentheses before the variable to be converted
The PHP data types that allow conversion are:
(int), (integer): converted to integer
(float), (double) , (real): Convert to floating point type
(string): Convert to string
(bool), (boolean): Convert to Boolean type
(array): Convert to array
(object): Convert to object
Example: Convert integer type to string type
<?php $int=31454; $str=(string)$int; var_dump($int); var_dump($str); ?>
Output:
int 31454 string '31454' (length=5)
2. Use strval() function
<?php $int=520; $str=strval($int); var_dump($int); var_dump($str); ?>
Output:
int 520 string '520' (length=3)
Description:
strval() function is used to get the string value of the variable (PHP version requirements: PHP 4, PHP 5, PHP 7). The syntax is as follows:
string strval ( mixed $var )
$var--can be any scalar type, but cannot be an array or object.
Return value: Returns a string.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert integer to string in php. For more information, please follow other related articles on the PHP Chinese website!