PHP: casting to a dynamically determined type

PhpCasting

Php Problem Overview


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

Php Solutions


Solution 1 - Php

Use the settype() function.

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

Example:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshealtielView Question on Stackoverflow
Solution 1 - PhpmaurisView Answer on Stackoverflow