2

Like the title says, I want to cast to a type from the value of a string, for instance:

$type = "int";
$str = "20 dogs";
echo ($type)$str;

Best regards

Saruman
  • 133
  • 1
  • 7

1 Answers1

4

Sure, though not in the way you are showing. There's a function for that:

settype($str, $type);
echo $str;

Also, for completeness (NOT RECOMMENDED):

eval("echo ($type)\$str;");
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87