3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php /** * Immutable class for handling numbers with precision. * * Never change the $number or the $precision after the creation of the object. */ class DecimalNumber { private $precision; private $number; /** * Get an instance given a string and a precision * @param string $string The decimal number in string form * @param int $precision The precision of the number * @return DecimalNumber Return new instance */ public static function string($string, $precision = 4) { $me = get_called_class(); $number = new $me; $number->precision = $precision; $number->number = bcadd($string, '0', $precision); return $number; } /** * Perform an addition operation * @param DecimalNumber $number the number to add * @return DecimalNumber Return the sum */ public function sum(DecimalNumber $number) { return DecimalNumber::string(bcadd($this->number, $number, $this->precision)); } public function floored() { return bcadd(bcadd($this->number, '0'), '0', $this->precision); } /** * Automagically convert to a string * @return string returns a string containing the value of this DecimalNumber */ public function __toString() { return $this->number; } } $d = DecimalNumber::createWithString('4.7'); echo $d->sum(DecimalNumber::createWithString('2.1')); echo $d->floored();
Output for 7.0.0 - 7.1.0
Fatal error: Uncaught Error: Call to undefined method DecimalNumber::createWithString() in /in/kmXes:52 Stack trace: #0 {main} thrown in /in/kmXes on line 52
Process exited with code 255.
Output for 5.2.10 - 5.6.28
Fatal error: Call to undefined method DecimalNumber::createWithString() in /in/kmXes on line 52
Process exited with code 255.
Output for 5.2.3 - 5.2.9
Fatal error: Call to undefined method DecimalNumber::createwithstring() in /in/kmXes on line 52
Process exited with code 255.
Output for 5.0.0 - 5.2.2
<br /> <b>Fatal error</b>: Call to undefined method DecimalNumber::createwithstring() in <b>/in/kmXes</b> on line <b>52</b><br />
Process exited with code 255.
Output for 4.4.2 - 4.4.9
<br /> <b>Parse error</b>: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in <b>/in/kmXes</b> on line <b>10</b><br />
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
<br /> <b>Parse error</b>: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in <b>/in/kmXes</b> on line <b>10</b><br />
Process exited with code 255.
Output for 4.3.2 - 4.3.4
<br /> <b>Parse error</b>: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in <b>/in/kmXes</b> on line <b>10</b><br />
Process exited with code 255.