Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!
The `strval()` function in PHP is used to get the string value of a variable. It returns the string value of a variable, regardless of its data type. If the variable is an object, it will return the object’s class name.
Here’s an example of how to use `strval()` in PHP:
$num = 123;
$str = strval($num);
echo $str; // Output: "123"
In the above example, we have a variable `$num` which is an integer. We use the `strval()` function to convert it to a string and store the result in the `$str` variable. Finally, we use the `echo` statement to output the string value of `$num`.
Similarly, we can use the `strval()` function to convert other data types to strings as well. For example:
$bool = true;
$str = strval($bool);
echo $str; // Output: "1"
In the above example, we have a boolean variable `$bool`. We use the `strval()` function to convert it to a string and store the result in the `$str` variable. Since `true` is equivalent to `1` in PHP, the output of the `echo` statement is `”1″`.