38 of 410 menu

The strval Function

The strval function returns the string representation of the passed value. It accepts any scalar value or object as a parameter, that can be converted to a string.

Syntax

strval(mixed $value): string

Example

Convert a number to a string:

<?php $num = 123; $res = strval($num); var_dump($res); ?>

Code execution result:

'123'

Example

Convert a boolean value to a string:

<?php $bool = true; $res = strval($bool); var_dump($res); ?>

Code execution result:

'1'

Example

Let's try to convert an array to a string:

<?php $arr = [1, 2, 3]; $res = strval($arr); var_dump($res); ?>

Code execution result:

'Array'

See Also

  • the is_string function,
    which checks if a variable is a string
  • the settype function,
    which changes the type of a variable
byenru