如何将PHP变量的值转换为字符串?
我正在寻找比连接一个空字符串更好的东西:
$myText = $myVar . '';
类似于Java或。net中的ToString()方法。
如何将PHP变量的值转换为字符串?
我正在寻找比连接一个空字符串更好的东西:
$myText = $myVar . '';
类似于Java或。net中的ToString()方法。
当前回答
文档说你还可以这样做:
$str = "$foo";
和演员一样,但是我觉得看起来更漂亮。
来源:
俄罗斯 英语
其他回答
This can be difficult in PHP because of the way data types are handled internally. Assuming that you don't mean complex types such as objects or resources, generic casting to strings may still result in incorrect conversion. In some cases pack/unpack may even be required, and then you still have the possibility of problems with string encoding. I know this might sound like a stretch but these are the type of cases where standard type juggling such as $myText = $my_var .''; and $myText = (string)$my_var; (and similar) may not work. Otherwise I would suggest a generic cast, or using serialize() or json_encode(), but again it depends on what you plan on doing with the string.
主要的区别在于,Java和. net在处理二进制数据和基本类型以及从特定类型转换到/从特定类型转换到字符串方面有更好的功能,即使对用户抽象了特定的情况也是如此。PHP的情况则完全不同,即使是处理十六进制也会让你摸不着头脑,直到你掌握了它。
我想不出更好的方法来回答这个问题,这是可比Java/。NET中的_toString()和此类方法通常以特定于对象或数据类型的方式实现。在这种情况下,神奇的方法__toString()和__serialize()/__unserialize()可能是最好的比较。
还要记住,PHP没有基本数据类型的相同概念。从本质上讲,PHP中的每一种数据类型都可以被认为是一个对象,它们的内部处理程序试图使它们具有某种通用性,即使这意味着在将float转换为int时失去准确性。你不能像在Java中那样处理类型,除非你在本地扩展中使用它们的zvals。
While PHP userspace doesn't define int, char, bool, or float as an objects, everything is stored in a zval structure which is as close to an object that you can find in C, with generic functions for handling the data within the zval. Every possible way to access data within PHP goes down to the zval structure and the way the zend vm allows you to handles them without converting them to native types and structures. With Java types you have finer grained access to their data and more ways to to manipulate them, but also greater complexity, hence the strong type vs weak type argument.
这些链接可能会有帮助:
https://www.php.net/manual/en/language.types.type-juggling.php https://www.php.net/manual/en/language.oop5.magic.php
使用print_r:
$myText = print_r($myVar,true);
你也可以这样使用它:
$myText = print_r($myVar,true)."foo bar";
这将把$myText设置为一个字符串,就像:
array (
0 => '11',
)foo bar
使用var_export来获得更多信息(变量类型,…):
$myText = var_export($myVar,true);
正如其他人所提到的,对象需要__toString方法来强制转换为字符串。没有定义该方法的对象仍然可以使用spl_object_hash函数生成字符串表示。
该函数返回对象的唯一标识符。这个id可以用作存储对象的散列键,或者用于标识一个对象,只要该对象没有被销毁。一旦对象被销毁,它的散列可以被其他对象重用。
我有一个带有__toString方法的基Object类,默认调用md5(spl_object_hash($this))以使输出明确唯一,因为spl_object_hash的输出在对象之间看起来非常相似。
这对于调试变量初始化为Object的代码特别有帮助,稍后在代码中它被怀疑已经更改为不同的Object。简单地将变量回显到日志中就可以显示(或不显示)来自对象哈希的更改。
我觉得这个问题有点误导人, toString()在Java中不仅仅是一种将某些东西强制转换为String的方法。这就是via (string)或string . valueof()所做的,它在PHP中也同样有效。
// Java
String myText = (string) myVar;
// PHP
$myText = (string) $myVar;
注意,这可能是有问题的,因为Java是类型安全的(有关详细信息,请参阅这里)。
但正如我所说,这是强制转换,因此不等同于Java的toString()。
Java中的toString不仅仅是将对象转换为String。相反,它将为您提供String表示形式。这就是PHP中的__toString()所做的。
// Java
class SomeClass{
public String toString(){
return "some string representation";
}
}
// PHP
class SomeClass{
public function __toString()
{
return "some string representation";
}
}
另一方面:
// Java
new SomeClass().toString(); // "Some string representation"
// PHP
strval(new SomeClass); // "Some string representation"
我说的“给予字符串表示”是什么意思? 想象一个有数百万册图书的图书馆的类。
将该类转换为String将(默认情况下)将数据(这里是所有书籍)转换为字符串,因此String将非常长,而且大多数情况下也不是很有用。 To String将为您提供String表示形式,即仅提供库的名称。这是更短的,因此给你更少,但更重要的信息。
这些都是有效的方法,但目标非常不同,都不是对每种情况的完美解决方案,您必须明智地选择更适合您的需求的解决方案。
当然,还有更多的选择:
$no = 421337 // A number in PHP
$str = "$no"; // In PHP, stuff inside "" is calculated and variables are replaced
$str = print_r($no, true); // Same as String.format();
$str = settype($no, 'string'); // Sets $no to the String Type
$str = strval($no); // Get the string value of $no
$str = $no . ''; // As you said concatenate an empty string works too
所有这些方法都将返回一个String,其中一些在内部使用__toString,而其他一些方法将在object上失败。查看PHP文档了解更多细节。
如果转换的不是简单类型(如整数或布尔值),则需要为要转换的类型编写自己的函数/方法,否则PHP将只打印该类型(如数组、GoogleSniffer或Bidet)。