The PHP strval() function converts a given variable to a string. It takes a scalar variable (like integers, floats, and booleans) and converts it to its string representation. For arrays, objects, and resources, it does not work and may produce unexpected results.
Syntax
strval( $variable )
Parameter: This function accepts a single parameter $variable. This parameter represents the value which we want to convert to a string
Return value: This function returns a string. This string is generated by typecasting the value of the variable passed to it as a parameter.
The below programs illustrate the strval() function.
Example: In this example we use of the strval() function, which converts the float value 32.360 to a string and prints it. The function ensures the variable is treated as a string.
PHP <?php $var_name = 32.360; // prints the value of above variable // as a string echo strval($var_name); ?>
Example : In this example we defines a class geeksforgeeks with a __toString() method that returns the class name. Using strval() on an instance of the class prints its name as a string.
PHP <?php class geeksforgeeks{ public function __toString(){ // returns the class name return __CLASS__; } } // prints the name of above class as // a string echo strval(new geeksforgeeks); ?>
Example : In this example we demonstrates the strval() function with an array as the parameter. Since strval() does not convert arrays to strings, it prints "Array," indicating the type, rather than converting the array's contents.
php <?php // 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); ?>
Similar Reads
PHP | gmp_strval() Function The gmp_strval() is an inbuilt function in PHP which returns the string value of a GMP number. (GNU Multiple Precision: For large numbers). Syntax: string gmp_strval ( GMP $num, int $base ) Parameters: The function accepts two parameters $num and $base as shown above and described below. $num - The
3 min read
PHP strlen() Function The strlen() is a built-in function in PHP which returns the length of a given string.It calculates the length of the string including all the whitespaces and special characters. Syntax:strlen($string);Parameters: This parameter represents the string whose length is needed to be returned. Return Val
1 min read
PHP rand() Function In this article, we will see how to get the random number using the rand() function in PHP, along with knowing its implementation through the example. The rand() is an inbuilt function in PHP used to generate a random number ie., it can generate a random integer value in the range [min, max]. Syntax
2 min read
PHP String Functions Strings are a fundamental data type in PHP, used to store and manipulate text. PHP provides a wide variety of built-in string functions. These functions perform various operations such as string transformations, character manipulations, encoding and decoding, and formatting, making string handling s
6 min read
PHP stristr() Function The stristr() function is a built-in function in PHP. It searches for the first occurrence of a string inside another string and displays the portion of the latter starting from the first occurrence of the former in the latter (before if specified). This function is case-insensitive. Syntax : strist
2 min read