3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Our fraction, 3/12, could be written better $numerator = 3; $denominator = 12; /** * @param int $num * @return array The common factors of $num */ function getFactors($num) { $factors = []; // get factors of the numerator for ($x = 1; $x <= $num; $x ++) { if ($num % $x == 0) { $factors[] = $x; } } return $factors; } /** * @param int $x * @param int $y */ function getGreatestCommonDenominator($x, $y) { // first get the common denominators of both numerator and denominator $factorsX = getFactors($x); $factorsY = getFactors($y); // common denominators will be in both arrays, so get the intersect $commonDenominators = array_intersect($factorsX, $factorsY); // greatest common denominator is the highest number (last in the array) $gcd = array_pop($commonDenominators); return $gcd; } // divide the numerator and denomiator by the gcd to get our refactored fraction $gcd = getGreatestCommonDenominator($numerator, $denominator); echo ($numerator / $gcd) .'/'. ($denominator / $gcd); // we can use divide (/) because we know result is an int :-)
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
1/4
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 1/4

preferences:
224.47 ms | 401 KiB | 299 Q