Home >Backend Development >PHP Problem >How to convert numbers to string in php?

How to convert numbers to string in php?

青灯夜游
青灯夜游Original
2020-08-26 11:25:193851browse

php method to convert numbers into strings: 1. Add the target type enclosed in parentheses before the variable to be converted, for example "(string)3.14"; 2. Use the strval() function, For example "strval(3.14)"; 3. Use the settype() function.

How to convert numbers to string in php?

Recommended: "PHP Video Tutorial"

PHP data type conversion (character conversion Numbers, numbers to characters)

PHP data type conversion is forced conversion. The PHP data types that are allowed to be converted are:

  • (int) , (integer): converted to integer

  • ## (float), (double), (real): converted to floating point type

  • ( string): Convert to string

  • (bool), (boolean): Convert to Boolean type

  • (array): Convert to array

  • (object): Convert to object

There are three conversion methods for PHP data types:

  • Add the target type enclosed in parentheses before the variable to be converted

  • Use three specific types of conversion functions, intval(), floatval(), strval() [Memory : The destination type you want to convert val()】

  • Use the general type conversion function settype(mixed var, string type)

The first conversion method: (int) (bool) (float) (string) (array) (object)

<?php   
$num1=3.14;   
$num2=(string)$num1;   
var_dump($num1); //输出float(3.14)   
var_dump($num2); //输出string(3.14)   
?>

Output:

float 3.14
string &#39;3.14&#39; (length=4)

The second conversion Method: intval() floatval() strval()

<?php   
$str="123.9abc";   
$int=intval($str);     //转换后数值:123
$float=floatval($str); //转换后数值:123.9
$str=strval($float);   //转换后字符串:"123.9"
var_dump($int); //输出int(123)   
var_dump($float); //输出float(123.9)
var_dump($str); //输出string(123.9)  
?>

Output:

int 123
float 123.9
string &#39;123.9&#39; (length=5)

The third conversion method: settype()

<?php   
$num4=12.8;   
$flg=settype($num4,"string");   
var_dump($flg);  //输出bool(true)   
var_dump($num4); //输出string(12.8)   
?>

Output:


boolean true
string &#39;12.8&#39; (length=4)

The settype() function is used to set the type of a variable.

PHP version requirements: PHP 4, PHP 5, PHP 7

Syntax

bool settype ( mixed &$var , string $type )

Parameter description:

  • $var: 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" (from PHP 4.2.0)

Return value:

Returns TRUE when the setting is successful, otherwise it fails Returns FALSE.

If you want to get more relevant knowledge, you can visit:

Programming Teaching

The above is the detailed content of How to convert numbers to string in php?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn