PHP | strval() Function

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 ) 

Parameter: This function accepts a single parameter $variable. This parameter represents the value which we want to convert to 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.

Below programs illustrate the strval() function.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
$var_name = 32.360;
  
// prints the value of above variable 
// as a string
echo strval($var_name);
  
?>

chevron_right


Output:

32.36

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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);
  
?>

chevron_right


Output:

geeksforgeeks

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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);
  
?>

chevron_right


Reference:
http://php.net/manual/en/function.strval.php



My Personal Notes arrow_drop_up

This article is contributed by sid4321 , Shivani2609. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : nidhi_biet



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.