I have an unknwon string that could resemble a float. In that case I want to convert it to float (for calculations), otherwise leave it as a string.
function StrToFloat($var){ if(is_numeric($var)){ return (float)$var; } else return $var; } $a = "1.23"; // convert $a to 1.23 $b = "1.2 to 1.3"; // leave $b as is $a = StrToFloat($a); // $a = 1.23 $b = StrToFloat($b); // $b = "1.2 to 1.3"