Open In App

How to convert an Integer Into a String in PHP ?

Last Updated : 22 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

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

Similar Reads

How to convert a string into a integer without using parseInt() function in JavaScript ?
In JavaScript, there is a simple function parseInt() to convert a string to an integer. In order to know more about the function, you can refer to this. In this article, we will learn how to convert the string to an integer without using the parseInt() function. Advantage of parseInt() over this method. The parseInt() function converts the number w
2 min read
Convert boolean result into number/integer in JavaScript
A JavaScript boolean represents one of two values: true or false. However, if one wants to convert a variable that stores boolean value, into integer "0" or "1", they can do so using multiple approaches. We will look into some of them in this article. The most popular methods are: Using ternary or conditional operator Using unary + operator. Using
3 min read
How to convert a String into Number in PHP ?
Strings in PHP can be converted to numbers (float/ int/ double) very easily. In most use cases, it won't be required since PHP does implicit type conversion. This article covers all the different approaches for converting a string into a number in PHP, along with their basic illustrations. There are many techniques to convert strings into numbers i
3 min read
Convert a String into an Array of Characters in PHP
Given a string, the task is to convert the string into an array of characters using PHP. Examples: Input: str = "GFG" Output: Array( [0] =&gt; G [1] =&gt; f [2] =&gt; G ) Input: str = "Hello Geeks" Output: Array( [0] =&gt; H [1] =&gt; e [2] =&gt; l [3] =&gt; l [4] =&gt; o [5] =&gt; [6] =&gt; G [7] =&gt; e [8] =&gt; e [9] =&gt; k [10] =&gt; s )There
3 min read
PHP Program to Convert Integer to Roman Number
Converting an integer to a Roman number is a common problem often encountered in applications dealing with dates, historical data, and numeral representations. In this article, we will explore various approaches to converting an integer to Roman numerals in PHP. Table of Content Using Simple Mapping ArrayUsing Recursive ApproachUsing Simple Mapping
2 min read
How to convert Integer array to String array using JavaScript ?
The task is to convert an integer array to a string array in JavaScript. Here are a few of the most used techniques discussed with the help of JavaScript. Approaches to convert Integer array to String array:using JavaScript array.map() and toString() methodsusing JavaScript Array.join() and split() methodsusing JavaScript Array.forEach() and String
2 min read
How to convert string of any base to an integer in JavaScript ?
Given a string containing an integer value and along with that user passes a base value. We need to convert that string of any base value to an integer in JavaScript. String Integer "1002" 1002 For performing the above-illustrated task, we would be using a method (or a function) provided by JavaScript called as parseInt(). This is a special method,
3 min read
Convert a String to an Integer in JavaScript
Converting a string to an integer in JavaScript involves transforming a string that represents a numeric value into an actual integer data type. This is particularly useful when you need to perform arithmetic operations or comparisons on numeric values stored as strings. There are various methods available for converting strings to integers, which
6 min read
How to convert an array into object using stdClass() in PHP?
To convert an array into the object, stdClass() is used. The stdClass() is an empty class, which is used to cast other types to object. If an object is converted to object, its not modified. But, if object type is converted/type-casted an instance of stdClass is created, if it is not NULL. If it is NULL, the new instance will be empty. Example 1: I
3 min read
How to convert XML file into array in PHP?
Given an XML document and the task is to convert an XML file into PHP array. To convert the XML document into PHP array, some PHP functions are used which are listed below: file_get_contents() function: The file_get_contents() function is used to read a file as string. This function uses memory mapping techniques which are supported by the server a
2 min read