What is the strval() function in PHP?

The strval() function is one of the many built-in functions that PHP has as part of its standard library. The strval() function converts integers, double, or float types into strings. strval() only does this on scalar values, which includes all of the aforementioned types. strval() cannot convert arrays and objects. If applied, strval() will only return the data type of the parsed value.

Syntax

strval( $variable )

Parameters

The variable that contains the value to be parsed is the only required parameter. The passed value will be converted into a string.

Return value

The strval() function typecasts the parameter variable into a string and returns it as a string.

Code

The program below illustrates the use of the strval() function.

<?php
$var_name = 32.360;
// prints the value of above variable
// as a string
echo strval($var_name)."\n";
echo gettype(strval($var_name))."\n";
// Program to illustrate the strval() function
// when an array is passed as parameter
// Input array
$arr = array(1,2,3,4,5);
// This will print only the type of value
// being converted i.e. 'Array'
echo strval($arr);
?>

You can see how using the gettype function on the $var variable, which is of datatype double, returns string after being passed into the strval() function. This confirms that it was converted into a string by the function.