Home > Article > Backend Development > What are the functions to convert numbers to strings in php?
Function to convert numbers to strings in php: 1. strval(), syntax format "strval (numeric variable)", which can obtain and return the string value of the variable; 2. settype(), syntax format " settype(numeric variable,'string')" can set the variable to the "string" type.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
digital conversion in php String
Method 1: Use strval() function
<?php $int_str= 123.52; var_dump($int_str); $str = strval($int_str); var_dump($str); ?>
Output:
float 123.52 string '123.52' (length=6)
Description:
The strval() function is a conversion function used to convert numerical values to strings. It can obtain and return the string value of a variable.
The strval() function converts the data type, but does not affect the type of the original variable.
Method 2: settype() function
<?php header('content-type:text/html;charset=utf-8'); $old = 500; echo "原数据类型" . gettype($old), '<br>'; $current = gettype(settype($old, 'string')); echo "现数据类型" . gettype($current), '<hr>'; $old = 500.25; echo "原数据类型" . gettype($old), '<br>'; $current = gettype(settype($old, 'string')); echo "现数据类型" . gettype($current), '<hr>'; ?>
Output:
Description: settype( ) function is used to set the type of a variable. The syntax is as follows:
settype ( $var , $type )
Set the type of variable var to type.
Parameters | Description |
---|---|
##var | To be converted Variables. The possible values of|
type |
##type are:
|
Recommended learning: "
PHP Video TutorialThe above is the detailed content of What are the functions to convert numbers to strings in php?. For more information, please follow other related articles on the PHP Chinese website!