PHP: casting to a dynamically determined type

ぐ巨炮叔叔 提交于 2019-12-18 13:52:43

问题


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 dynamically determined type?

Thanks


回答1:


Use the settype() function.

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

Example:

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


来源:https://stackoverflow.com/questions/4687967/php-casting-to-a-dynamically-determined-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!