我问自己是否可以将字符串转换为之前定义的另一种类型
例如
$type = "int";
$foo = "5";
$bar = ($type) $foo;
以及$ bar === 5
是的,有一个内置函数:
$type = "int";
$foo = "5";
settype($foo, $type); // $foo is now the int 5
请注意,的返回值settype()
是操作的成功状态,而不是转换后的变量。感谢下面的@NRVM。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句