Home  >  Article  >  Backend Development  >  How to convert integer to string in php

How to convert integer to string in php

青灯夜游
青灯夜游Original
2021-03-17 18:11:405066browse

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.

How to convert integer to string in php

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 &#39;31454&#39; (length=5)

2. Use strval() function

<?php   
$int=520;   
$str=strval($int);   
var_dump($int); 
var_dump($str); 
?>

Output:

int 520
string &#39;520&#39; (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!

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