【问题标题】:PHP: casting to a dynamically determined typePHP:强制转换为动态确定的类型
【发布时间】:2011-01-14 04:25:03
【问题描述】:

我想做这样的事情:

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

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

谢谢

【问题讨论】:

    标签: php casting


    【解决方案1】:

    使用settype() 函数。

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

    例子:

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

    【讨论】:

    • 碰巧做了 Google-Open-Copy-Paste 过程。
    猜你喜欢
    • 2012-07-06
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    • 2021-01-24
    • 2015-10-26
    相关资源
    最近更新 更多