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.
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.
You can always create a method named .ToString($in) that returns
$in . '';
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.
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.
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.
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).
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.
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);
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.
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().
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.
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