Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
communitycreator

What is the strval() function in PHP?

NDUKWE CHIDERA K.

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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.

RELATED TAGS

php
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring