public function big_integer_add($num1,$num2){ $str1 = strval($num1); $str2 = strval($num2); $length1 = strlen($str1); $length2 = strlen($str2); $length = $length1>$length2?$length1:$length2; $result = ''; $flag = 0; while($length--){ $m = 0; $n = 0; if($length1>0) $m = $str1[--$length1]; if($length2>0) $n = $str2[--$length2]; $tmp = $m+$n+$flag; $flag = $tmp/10; $result = ($tmp%10).$result; } return doubleval($result);//或者直接返回字符串 }
$a = 9874651230987465123098746512309874651230987465123098746512309874651230987465123098746512309874651230; $b = 9874651230987465123098746512309874651230987465123098746512309874651230987465123098746512309874651230; dump($this->big_integer_add($a,$b)); echo "<br>"; dump($a+$b) ;