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

How to convert integer to string type in php?

青灯夜游
青灯夜游Original
2020-10-07 10:51:255697browse

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.

How to convert integer to string type in php?

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:

How to convert integer to string type in php?

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 &#39;545248&#39; (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 &#39;545248&#39;

?>

Output:

boolean true
string &#39;545248&#39; (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!

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