Horje
How to convert an Integer Into a String in PHP ?

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.

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

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

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

Using sprintf() Function

The sprintf() function writes a formatted string to a variable.

Syntax:

$str = sprintf("%d", $var_name);

Example: 

PHP
<?php 
$var_name = 2; 

// Using sprintf to convert integer to string 
$str = sprintf("%d", $var_name); 

// Prints the value of above variable as a string 
echo "Welcome $str GeeksforGeeks"; 
?>

Output:

Welcome 2 GeeksforGeeks

Using concatenation with an Empty String

Concatenating an integer with an empty string in PHP converts the integer to a string. This approach leverages the automatic type conversion in PHP, making it simple and intuitive. For example, `$string = $integer . “”;` effectively turns `$integer` into a string.

Example:

PHP
<?php
$integer = 123;
$string = $integer . "";
echo $string; // Outputs: 123
?>

Output
123

Using settype() Function

Another method to convert an integer to a string in PHP is by using the settype() function. This function sets the type of a variable, converting it to the specified type.

Syntax:

settype($variable, "string");

Example:

PHP
<?php
$integer = 1000;
settype($integer, "string");
echo "Converted string = " . $integer;
?>

Output
Converted string = 1000





Reffered: https://www.geeksforgeeks.org


PHP

Related
How to check if a String Contains a Substring in PHP ? How to check if a String Contains a Substring in PHP ?
AsgardCMS - Introduction of in-built and ready to use modules AsgardCMS - Introduction of in-built and ready to use modules
PHP SplPriorityQueue extract() Function PHP SplPriorityQueue extract() Function
PHP SplPriorityQueue current() Function PHP SplPriorityQueue current() Function
PHP SplPriorityQueue count() Function PHP SplPriorityQueue count() Function

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
8