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

phpstrval(mixed $value): string

Example

Convert a number to a string:

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

Code execution result:

php'123'

Example

Convert a boolean value to a string:

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

Code execution result:

php'1'

Example

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

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

Code execution result:

php'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