3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function toFraction($number){ $numerator = $number < 0 ? -1 : 1; $denominator = 0; for(; $numerator < 1000; $numerator++){ $temp = $numerator / $number; if(ceil($temp) - $temp == 0){ $denominator = $temp; break; } } return ($denominator > 0) ? $numerator . '/' . $denominator : false; } echo toFraction( -0.5) . "\n" . toFraction( .5);
Output for 4.3.0 - 7.1.0
-1/2 1/2