PHP:转换为动态确定的类型

2022-08-30 10:16:51

我想做这样的事情:

$type = "int";
$casted = ($type)$value;

这不起作用。但是,是否有其他方法可以转换为动态确定的类型?

谢谢


答案 1

使用该函数。settype()

http://php.net/manual/en/function.settype.php

例:

$type = 'int';
$var = '20';
settype($var, $type);
var_dump($var);

答案 2

推荐