PHP equivalent of .NET/Java's toString()

Antoine Aubry picture Antoine Aubry · Aug 26, 2008 · Viewed 1.5M times · Source

How do I convert the value of a PHP variable to string?

I was looking for something better than concatenating with an empty string:

$myText = $myVar . '';

Like the ToString() method in Java or .NET.

Answer

Tom Mayfield picture Tom Mayfield · Aug 26, 2008

You can use the casting operators:

$myText = (string)$myVar;

There are more details for string casting and conversion in the Strings section of the PHP manual, including special handling for booleans and nulls.