How to Convert an Integer into a String with PHP

In this short guide, we will assist you in dealing with one of the common issues in PHP: converting an integer into a string. Here, we will represent to you how to do it using the strval() function. First of all, let’s check out the syntax of the strval() function: xstrval( $variable )

Below, you will find several options to use the function:

<?php

$var_name = 55.721;

// prints the value of above variable
// as a string
echo strval($var_name);

?>

Here is the next of getting a string:

<?php

class w3Docs
{
  public function __toString()
  {
    // returns the class name
    return __CLASS__;
  }
}

// printing the name of the class above as
// a string
echo strval(new w3Docs());

?>

And, here is the final one:

<?php
// Program for illustrating the strval() function
// once an array is passed as parameter

// Input array
$arr = [1, 2, 3, 4, 5];

// It prints just the type of value
// being converted i.e. 'Array'
echo strval($arr);

?>

Definition of the Strval() Function

This is an inbuilt PHP function aimed at converting every scalar value such as integer, string, double into a string. You can’t use the xstrval() function on arrays or objects. If you do so, the function will only return the type name of the returned value.

This function accepts a single parameter $variable. The latter presents the value, which you intend to convert.