W3docs

strval()

The strval() function is a built-in function in PHP that converts a value to a string. It can be used to convert any data type to a string data type.

Introduction

strval() is a built-in PHP function that returns the string representation of a scalar value. It is the explicit, readable way to say "give me this value as a string" — the same conversion PHP performs implicitly when you echo a value or concatenate it with ., but written out so your intent is clear.

This page covers what strval() returns for each data type, how it handles null, booleans, floats, arrays, and objects, and when you should reach for it instead of a cast or one of its siblings such as intval(), floatval(), and boolval().

Syntax

strval(mixed $value): string

strval() takes a single argument, $value, and returns a string that represents it. It works on any scalar (int, float, bool, string), on null, and on objects that implement __toString(). Passing an array produces the literal string "Array" and raises a warning (see Edge cases).

Basic example

Convert an integer, a float, and a boolean to strings:

php— editable, runs on the server

$var1 is an integer, $var2 a float, and $var3 a boolean. Each is converted to its string form. Note that true becomes "1" — that surprises many beginners, and it is the single most important quirk to remember.

How each type is converted

strval() follows PHP's standard type-juggling rules. The table below lists the result for every scalar type and for null.

Input valuestrval() resultNotes
10 (int)"10"Digits as written
3.14 (float)"3.14"Uses precision ini setting
true (bool)"1"
false (bool)""An empty string, not "0"
null""Empty string
"hello" (string)"hello"Returned unchanged

The false → "" and null → "" rules are worth memorizing — they are the classic cause of "why is my output blank?" bugs:

<?php
var_dump(strval(false));  // string(0) ""
var_dump(strval(null));   // string(0) ""
var_dump(strval(true));   // string(1) "1"
?>

strval() vs. casting vs. concatenation

These three produce the same result for scalars, so the choice is about readability:

<?php
$n = 42;

$a = strval($n);   // "42" — explicit, named, greppable
$b = (string) $n;  // "42" — language cast
$c = "$n";         // "42" — string interpolation
$d = $n . "";      // "42" — concatenation trick (avoid)
?>

Prefer strval() or the (string) cast when you want to be explicit. strval() shines in one situation a cast cannot match: it is a callable, so you can pass it to higher-order functions like array_map():

<?php
$numbers = [1, 2, 3, 4];

// Turn every element into a string in one line.
$strings = array_map('strval', $numbers);

print_r($strings);
// Array
// (
//     [0] => 1
//     [1] => 2
//     [2] => 3
//     [3] => 4
// )

var_dump($strings[0]); // string(1) "1"
?>

You cannot write array_map('(string)', $numbers) — there is no cast callable — so strval is the clean way to map a cast over a collection.

Floats: watch the precision

Floats are formatted using PHP's precision ini directive (14 significant digits by default), and very large or very small numbers switch to scientific notation. This means strval() is not the right tool for displaying money or fixed-decimal output — use number_format() or sprintf() for that.

<?php
echo strval(0.1 + 0.2) . "\n";   // 0.3   (precision rounds the float error)
echo strval(1.0) . "\n";         // 1     (trailing ".0" is dropped)
echo strval(1.0e21) . "\n";      // 1.0E+21  (scientific notation)

// For a fixed two-decimal price, use sprintf instead:
echo sprintf('%.2f', 1.0) . "\n"; // 1.00
?>

Edge cases: arrays and objects

strval() is meant for scalars. Two non-scalar cases behave specially:

Arrays convert to the literal string "Array" and trigger a warning — almost never what you want. To turn an array into text use implode() or json_encode():

<?php
$data = [1, 2, 3];

echo strval($data);            // Warning: Array to string conversion -> "Array"
echo implode(', ', $data);     // 1, 2, 3
echo json_encode($data);       // [1,2,3]
?>

Objects convert by calling their __toString() method. If the class does not define one, PHP throws an Error:

<?php
class Money
{
    public function __construct(private float $amount) {}

    public function __toString(): string
    {
        return sprintf('$%.2f', $this->amount);
    }
}

echo strval(new Money(5)); // $5.00
?>

When to use strval()

  • Mapping a cast over a collection with array_map('strval', $items) — the standout use case.
  • Forcing string semantics before a strict comparison (===) or before passing to an API or function that type-hints string.
  • Self-documenting code, where a named strval() reads more clearly than a bare (string) cast.

For the reverse conversions, see the companion functions intval(), floatval(), and boolval(). To inspect a value's type before converting, use gettype() or change it in place with settype().

Conclusion

strval() returns the string form of a scalar value, mirroring PHP's implicit conversion rules but stating your intent explicitly. Remember its three quirks: true becomes "1", both false and null become the empty string "", and arrays become the warning-laden "Array". Reach for it when you need a string-conversion callable (especially with array_map), and reach for number_format() or sprintf() when you need controlled numeric formatting instead.

Practice

Practice
What does the strval() function in PHP do?
What does the strval() function in PHP do?
Was this page helpful?