PHP strval() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

In PHP, strval() returns the string value of a variable. It converts integers, floats, booleans and null to text, and it is the function version of the (string) cast.

You will use strval() when you need a real string, for example before you pass a number to a function that expects text, or when you want to pad or compare values as text. It does not change the original variable. It returns a new string.

Syntax

Call strval() with the values you need. Start with this line.

PHP
$text = strval($value);
CallWhat it does
strval(123)Returns the string '123'.
strval(true)Returns the string '1'.
strval(null)Returns an empty string.

strval() works the same as (string) $value. It can convert an object only if its class has a __toString() method. Converting an array gives the word Array and a warning, so avoid it.

Library declarations
PHP
strval(mixed $value): string

Parameters

ParameterDescription
valueRequired. The value to convert. It can be any scalar type or an object with __toString(). Do not pass an array.

Return Value

Return ValueDescription
stringThe string version of the value. true becomes '1', while false and null become an empty string.

PHP Examples

Example 1 — Converting Common Values

Convert an integer, a float, true, false and null, and look at the exact string each one gives.

PHP
<?php
var_dump(strval(123));
var_dump(strval(1.5));
var_dump(strval(true));
var_dump(strval(false));
var_dump(strval(null));

How It Works

  1. 123 becomes '123'. The string has three characters.
  2. 1. 5 becomes '1.5'. The decimal point is part of the text, so the length is 3.
  3. true becomes '1', but false becomes an empty string. This is a common surprise.
  4. null becomes an empty string. Its length is 0, just like false.

Example 2 — Objects and Special Floats

An object works if its class defines __toString(). A float with no decimals loses its point, and huge floats use scientific notation.

PHP
<?php
class Name {
    public function __toString() {
        return 'Asha';
    }
}

var_dump(strval(new Name()));
var_dump(strval(1.0));
var_dump(strval(1e25));

How It Works

  1. The Name class defines __toString(). strval() calls it and returns its text, Asha.
  2. 1. 0 becomes '1'. PHP drops the pointless .0 when it turns a float into text.
  3. 1e25 is too big to write out in full. PHP uses the scientific form 1.0E+25.
  4. Each result is a real string. var_dump() shows its length, such as string(4).

Example 3 — Padding and Joining Numbers

Turn numbers into strings so you can pad them, count their characters and join them as text.

PHP
<?php
$number = 7;

$padded = str_pad(strval($number), 3, '0', STR_PAD_LEFT);
echo $padded, PHP_EOL;

echo strlen(strval(12345)), PHP_EOL;
echo strval(3) . strval(4), PHP_EOL;

How It Works

  1. strval(7) gives the text '7'. str_pad() works on strings, so we convert first.
  2. str_pad() adds zeros on the left. The result is 007, which is always 3 characters long.
  3. strlen(strval(12345)) counts digits. The number has 5 digits, so it prints 5.
  4. The dot joins two strings. '3' and '4' become '34', not the sum 7.

Frequently Asked Questions

It converts a value to a string. For example strval(123) returns '123' and strval(true) returns '1'. It is the function form of the (string) cast.
They give the same result. strval() is a function, so it can be passed as a callback, for example array_map('strval', $numbers). The cast is shorter to write.
An empty string. strval(true) returns '1', which surprises many beginners. strval(null) is also an empty string.
No. An array gives the text 'Array' and a warning. Use implode() to join the items, or json_encode() to get a JSON string.
Only if the class has a __toString() method. Without it, PHP throws an Error saying that the object could not be converted to a string.

Next: unserialize()

unserialize() turns a string created by serialize() back into a PHP value. It can rebuild .

unserialize() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful