Oihana PHP

toString.php

Table of Contents

Functions

toString()  : string
Converts a value to a string representation.

Functions

toString()

Converts a value to a string representation.

toString(mixed $value) : string
  • Returns an empty string for null values.
  • Preserves the sign of -0.0 as the string "-0".
  • Converts arrays recursively by flattening and joining with commas.
  • Accepts objects implementing Stringable.
Parameters
$value : mixed

The value to convert.

Tags
example
echo toString(null);        // Outputs: ""
echo toString("hello");     // Outputs: "hello"
echo toString(123);         // Outputs: "123"
echo toString(-0.0);        // Outputs: "-0"
echo toString([1, 2, 3]);   // Outputs: "1,2,3"
echo toString([[1, 2], 3]); // Outputs: "1,2,3"

With Stringable object:

class Foo implements Stringable
{
    public function __toString() { return "foo"; }
}
echo toString(new Foo()); // Outputs: "foo"
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
string

The string representation of the value.


        
On this page

Search results