views:

64450

answers:

12

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.

+2  A: 

Does putting it in double quotes work?

$myText = "$myVar";
Mark Biek
A: 

You can always create a method named .ToString($in) that returns
$in . '';

Joel Coehoorn
A: 

Does putting it in double quotes work?

That works, but I don't know if it is the standard way of doing it in PHP.

Antoine Aubry
+10  A: 

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

A value can be converted to a string using the (string) cast or the strval() function. (Edit: As Thomas also stated).

It also should be automatically casted for you when you use it as a string.

Chris
+42  A: 

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.

Thomas G. Mayfield
A: 

Are you converting integers or something else? If you're converting anything other than simple types like integers or booleans, you'd need to write your own function/method for the type that you're trying to convert, otherwise PHP will just print the type (such as array, GoogleSniffer, or Bidet).

Brian Warshaw
A: 

PHP is dynamically typed, so like Chris Fournier said, "If you use it like a string it becomes a string".

If you're looking for more controll over the format of the string then printf is your answer.

Allain Lalonde
+42  A: 

In a class you can definie what is output by using the magical method __toString. An example is below:

class Example {

    private $output;

    public function __construct()
    {
        $this->output = 'Ninety nine green bottles';
    }

    public function __toString()
    {
        return $this->output;
    }

    $ex = new Example;
    $ex2 = str_replace('green', 'red', (string) $ex);
    echo((string) $ex . "\n" . $ex2);
}

Some more type casting examples:

$i = 1;

// "int" 1
var_dump((int) $i);

// "bool" true
var_dump((bool) $i);

// "string" 1
var_dump((string) 1);
Ross
+3  A: 

For primitives just use (string)$var or print this variable straight away. PHP is dynamically typed language and variable will be casted to string on the fly.

If you want to convert objects to strings you will need to define __toString() method that returns string. This method is forbidden to throw exceptions.

Michał Rudnicki
+2  A: 

this might be what you are looking for

http://ar.php.net/manual/en/function.strval.php

string strval ( mixed $var )

Get the string value of a variable. See the documentation on string for more information on converting to string.

This function performs no formatting on the returned value. If you are looking for a way to format a numeric value as a string, please see sprintf() or number_format().

opensas
+8  A: 

You can either use typecasting:

$var = (string)$varname;

or StringValue:

$var = strval($varname);

or SetType:

$var = settype($varname, 'string');

They all work for the same thing in terms of Type-Juggling.

Joel Larson
+1  A: 

I haven't seen this answer, so here it is :

$myText = print_r($myVar,true);

You can also use like

$myText = print_r($myVar,true)."foo bar";

Hope it helps :D

Cedric
"when the return parameter is TRUE, [print_r] will return a string."As print_r is a nice way to print objects, arrays (and also numbers/strings), it is a good way to transform an object into a human-readable string.
Cedric