开发者

PHP: casting to a dynamically determined type

开发者 https://www.devze.com 2023-02-04 12:29 出处:网络
I\'d like do something like this: $type = \"int\"; $casted = ($type)$value; This doesn\'t work. But is there some other way to cast to a dynamic开发者_如何学Pythonally determined type?

I'd like do something like this:

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

This doesn't work. But is there some other way to cast to a dynamic开发者_如何学Pythonally determined type?

Thanks


Use the settype() function.

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

Example:

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

精彩评论

暂无评论...
验证码 换一张
取 消