3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Fraction { const FRACTION_RATIO_ADJUST = true; const FRACTION_RATIO_KEEP = false; const FRACTION_FORMAT_DELIMITER = '/'; private $value = []; public function __construct($x, $y, $adjustRatio=self::FRACTION_RATIO_KEEP) { if(!is_int($x) || !is_int($y) || !$y) { throw InvalidArgumentException('Invalid fraction parts'); } $sign = ($x>0&&$y>0) || ($x<0&&$y<0)?1:-1; $this->value = ['numerator'=>$sign*$x, 'denominator'=>abs($y)]; if($adjustRatio==self::FRACTION_RATIO_ADJUST) { $this->adjustRatio(); } } public function getNumerator() { return $this->value['numerator']; } public function getDenominator() { return $this->value['denominator']; } public function adjustRatio() { $gcd = (int)gmp_strval(gmp_gcd( (string)$this->getNumerator(), (string)$this->getDenominator())); $this->setFraction([ 'numerator' => $this->getNumerator()/$gcd, 'denominator' => $this->getDenominator()/$gcd ]); } public function add(self $fraction, $adjustRatio=self::FRACTION_RATIO_KEEP) { $numerator = $this->getNumerator()*$fraction->getDenominator() + $this->getDenominator()*$fraction->getNumerator(); $denominator = $this->getDenominator()*$fraction->getDenominator(); $this->setFraction([ 'numerator' => $numerator, 'denominator' => $denominator ]); if($adjustRatio==self::FRACTION_RATIO_ADJUST) { $this->adjustRatio(); } } public function format() { $denominator = $this->getDenominator(); $numerator = $this->getNumerator(); return $denominator==1? (string)$numerator: (string)($numerator.self::FRACTION_FORMAT_DELIMITER.$denominator); } public function __toString() { return $this->format();//may be other } private function setFraction(array $data) { if(!array_key_exists('numerator', $data) || !array_key_exists('denominator', $data)) { throw InvalidArgumentException('Invalid data structure used in direct set'); } if(!is_int($data['numerator']) || !is_int($data['denominator']) || !$data['denominator']) { throw InvalidArgumentException('Invalid fraction data used in direct set'); } $this->value['numerator'] = $data['numerator']; $this->value['denominator'] = $data['denominator']; } } $fraction = new Fraction(8, -12, Fraction::FRACTION_RATIO_KEEP); echo($fraction->format());//-8/12 $fraction->add(new Fraction(1, 3), Fraction::FRACTION_RATIO_ADJUST); echo($fraction->format());//-1/3
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.34, 5.6.0 - 5.6.20, 5.6.28, 7.0.0 - 7.0.5, 7.0.14 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.25, 7.4.27 - 7.4.32, 8.0.0 - 8.0.12, 8.0.14 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.7
-8/12-1/3
Output for 7.0.6 - 7.0.10, 7.3.32 - 7.3.33, 7.4.26, 7.4.33, 8.0.13
-8/12 Fatal error: Uncaught Error: Call to undefined function gmp_strval() in /in/PaNiq:38 Stack trace: #0 /in/PaNiq(58): Fraction->adjustRatio() #1 /in/PaNiq(93): Fraction->add(Object(Fraction), true) #2 {main} thrown in /in/PaNiq on line 38
Process exited with code 255.
Output for 5.5.35 - 5.5.38, 5.6.21 - 5.6.25
-8/12 Fatal error: Call to undefined function gmp_strval() in /in/PaNiq on line 38
Process exited with code 255.
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/PaNiq on line 10
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/PaNiq on line 10
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/PaNiq on line 5
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/PaNiq on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/PaNiq on line 5
Process exited with code 255.

preferences:
245.61 ms | 401 KiB | 372 Q