Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: casting to a dynamically determined type

Tags:

php

casting

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

like image 783
shealtiel Avatar asked Jan 14 '11 04:01

shealtiel


1 Answers

Use the settype() function.

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

Example:

$type = 'int'; $var = '20'; settype($var, $type); var_dump($var); 
like image 131
mauris Avatar answered Oct 03 '22 07:10

mauris