How to convert integer to string type in php?

青灯夜游
Release: 2023-03-06 13:30:01
Original
5649 people have browsed it

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)  
?>
Copy after login

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); 
?>
Copy after login

Output:

string &#39;545248&#39; (length=6)
Copy after login

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 )
Copy after login

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;

?>
Copy after login

Output:

boolean true
string &#39;545248&#39; (length=6)
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template