这是通过类型转换完成的:$strvar = (string) $var; // Casts to stringecho $var; // Will cast to string implicitlyvar_dump($var); // Will show the true type of the variable在类中,您可以使用魔法方法定义输出内容__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"一些更多的类型转换示例:$i = 1;// int 1var_dump((int) $i);// bool truevar_dump((bool) $i);// string "1"var_dump((string) 1);