Using PHP's strval Function to Convert Variables to Strings

This article explains PHP's built‑in strval function, showing how it converts integers, floats, booleans, arrays, and objects into string representations, provides sample code, and highlights important considerations such as the default “Array” and “Object” outputs and alternative methods like json_encode.

php Courses
php Courses
php Courses
Using PHP's strval Function to Convert Variables to Strings

In PHP development, converting variables to string type is a common requirement. The built‑in strval function provides a straightforward way to achieve this conversion.

The strval function takes a single argument and returns its string representation. If the argument is already a string, it is returned unchanged; otherwise, the function converts the value based on its type.

Below is a simple example demonstrating how strval converts variables of different types to strings:

<?php

// Convert an integer to a string
$number = 123;
$str_number = strval($number);
echo gettype($str_number); // outputs "string"
echo $str_number; // outputs "123"

echo "<br>";

// Convert a float to a string
$float = 3.14;
$str_float = strval($float);
echo gettype($str_float); // outputs "string"
echo $str_float; // outputs "3.14"

echo "<br>";

// Convert a boolean to a string
$bool = true;
$str_bool = strval($bool);
echo gettype($str_bool); // outputs "string"
echo $str_bool; // outputs "1"

echo "<br>";

// Convert an array to a string
$array = [1, 2, 3];
$str_array = strval($array);
echo gettype($str_array); // outputs "string"
echo $str_array; // outputs "Array"

echo "<br>";

// Convert an object to a string
class Person {
    public $name = "John";
    public $age = 30;
}
$person = new Person();
$str_person = strval($person);
echo gettype($str_person); // outputs "string"
echo $str_person; // outputs "Object"
?>

The example shows that strval can handle integers, floats, booleans, arrays, and objects, returning a string representation for each. For scalar types the resulting string contains the value (e.g., "123" or "3.14"). For arrays and objects the function returns the type name "Array" or "Object".

When converting arrays or objects, strval does not serialize the actual data. If you need the concrete values, you should use other methods such as json_encode to obtain a JSON string representation.

In summary, strval is a useful PHP function for quickly converting variables to strings. It works well for most scalar types, and for complex types you can combine it with functions like json_encode to achieve the desired output.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPStringtype conversionstrval
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.