PHP 相当于 .NET/Java 的 toString()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP equivalent of .NET/Java's toString()
提问by Antoine Aubry
How do I convert the value of a PHP variable to string?
如何将 PHP 变量的值转换为字符串?
I was looking for something better than concatenating with an empty string:
我正在寻找比连接空字符串更好的东西:
$myText = $myVar . '';
Like the ToString()method in Java or .NET.
就像ToString()Java 或 .NET 中的方法一样。
回答by Tom Mayfield
You can use the casting operators:
您可以使用转换运算符:
$myText = (string)$myVar;
There are more details for string casting and conversion in the Strings sectionof the PHP manual, including special handling for booleans and nulls.
PHP 手册的字符串部分有更多有关字符串转换和转换的详细信息,包括对布尔值和空值的特殊处理。
回答by Ross
This is done with typecasting:
这是通过类型转换完成的:
$strvar = (string) $var; // Casts to string
echo $var; // Will cast to string implicitly
var_dump($var); // Will show the true type of the variable
In a class you can define what is output by using the magical method __toString. An example is below:
在类中,您可以使用神奇的方法定义输出内容__toString。一个例子如下:
class Bottles {
public function __toString()
{
return 'Ninety nine green bottles';
}
}
$ex = new Bottles;
var_dump($ex, (string) $ex);
// Returns: instance of Bottles and "Ninety nine green bottles"
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);
回答by Cedric
Use print_r:
使用print_r:
$myText = print_r($myVar,true);
You can also use it like:
你也可以像这样使用它:
$myText = print_r($myVar,true)."foo bar";
This will set $myTextto a string, like:
这将设置$myText为一个字符串,例如:
array (
0 => '11',
)foo bar
Use var_exportto get a little bit more info (with types of variable,...):
使用var_export获取更多信息(变量类型,...):
$myText = var_export($myVar,true);
回答by Joel Larson
You can either use typecasting:
您可以使用类型转换:
$var = (string)$varname;
or StringValue:
或字符串值:
$var = strval($varname);
or SetType:
或设置类型:
$success = settype($varname, 'string');
// $varname itself becomes a string
They all work for the same thing in terms of Type-Juggling.
在 Type-Juggling 方面,它们都为同一件事工作。
回答by Chris
How do I convert the value of a PHP variable to string?
如何将 PHP 变量的值转换为字符串?
A value can be converted to a string using the (string) castor the strval()function. (Edit: As Thomasalso stated).
可以使用(string) cast或strval()函数将值转换为字符串。(编辑:正如托马斯也所说)。
It also should be automatically casted for you when you use it as a string.
当您将它用作字符串时,它也应该自动为您转换。
回答by opensas
You are looking for strval:
您正在寻找strval:
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().
string strval ( mixed $var )获取变量的字符串值。有关转换为字符串的更多信息,请参阅字符串文档。
此函数不对返回值执行格式化。如果您正在寻找一种将数值格式化为字符串的方法,请参阅 sprintf() 或 number_format()。
回答by Micha? Rudnicki
For primitives just use (string)$varor print this variable straight away. PHP is dynamically typed language and variable will be casted to string on the fly.
对于原语,只需立即使用(string)$var或打印此变量。PHP 是动态类型语言,变量将被动态转换为字符串。
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.
如果要将对象转换为字符串,则需要定义__toString()返回字符串的方法。该方法禁止抛出异常。
回答by Mark Biek
Putting it in double quotes should work:
把它放在双引号中应该有效:
$myText = "$myVar";
回答by Daan
I think it is worth mentioning that you can catch any output (like print_r, var_dump) in a variable by using output buffering:
我认为值得一提的是,您可以通过使用输出缓冲来捕获变量中的任何输出(如print_r, var_dump):
<?php
ob_start();
var_dump($someVar);
$result = ob_get_clean();
?>
Thanks to: How can I capture the result of var_dump to a string?
回答by Justin Weeks
Another option is to use the built in settypefunction:
另一种选择是使用内置的settype函数:
<?php
$foo = "5bar"; // string
$bar = true; // boolean
settype($foo, "integer"); // $foo is now 5 (integer)
settype($bar, "string"); // $bar is now "1" (string)
?>
This actually performs a conversion on the variable unlike typecasting and allows you to have a general way of converting to multiple types.
与类型转换不同,这实际上对变量执行转换,并允许您有一种转换为多种类型的通用方法。

