The Wayback Machine - https://web.archive.org/web/20231120155125/https://www.geeksforgeeks.org/how-to-convert-an-integer-into-a-string-in-php/
Open In App
Related Articles

How to convert an Integer Into a String in PHP ?

Improve Article
Improve
Save Article
Save
Like Article
Like

The PHP strval() function is used to convert an Integer Into a String in PHP. There are many other methods to convert an integer into a string. 

In this article, we will learn many methods.

Methods:

  • Using strval() function.
  • Using inline variable parsing.
  • Using explicit Casting.

Method 1: Using strval() function.

Note:  The strval() function is an inbuilt function in PHP and is used to convert any scalar value (string, integer, or double) to a string. We cannot use strval() on arrays or on object, if applied then this function only returns the type name of the value being converted.

Syntax:

strval( $variable ) 

Return value: This function returns a string. This string is generated by typecasting the value of the variable passed to it as a parameter.

Example: 

PHP




<?php
    
$var_name = 2;
  
// converts integer into string
$str =  strval($var_name);
  
// prints the value of above variable as a string
echo "Welcome $str GeeksforGeeks";
  
?>


Output

Welcome 2 GeeksforGeeks

Method 2: Using Inline variable parsing.

Note: When you use Integer inside a string, then the Integer is first converted into a string and then prints as a string.

Syntax: 

$integer = 2;
echo "$integer";

Example:

PHP




<?php
    
$var_name = 2;
  
  
// prints the value of above variable
// as a string
echo "Welcome $var_name GeeksforGeeks";
  
?>


Output

Welcome 2 GeeksforGeeks

Method 3: Using Explicit Casting.

Note: Explicit Casting is the explicit conversion of data type because the user explicitly defines the data type in which he wants to cast. We will convert Integer into String.

Syntax:

$str = (string)$var_name

Example:

PHP




<?php
    
$var_name = 2;
  
//Typecasting Integer into string
$str = (string)$var_name;
  
// prints the value of above variable as a string
echo "Welcome $str GeeksforGeeks";
  
?>


Output

Welcome 2 GeeksforGeeks

 


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 30 Apr, 2021
Like Article
Save Article
Similar Reads
Related Tutorials