PHP: casting to a dynamically determined type

Use the settype() function.

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

Example:

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