A function to handle integers of any length, including negatives. Returns remainder but also calculates division in the process (could be useful in some cases).
<?php
function remainder($dividend, $divisor) {
if ($dividend == 0 || $divisor == 0) return 0;
$dividend .= '';
$remainder = 0;
$division = '';
while ($dividend < 0) {
$dividend += $divisor;
if ($dividend >= 0) return $dividend;
}
while (($remainder.$dividend)*1 > $divisor) {
while ($remainder*1 < $divisor) {
$remainder .= $dividend[0];
$remainder *= 1;
$dividend = substr($dividend, 1);
}
$mult = floor($remainder / $divisor);
$division .= $mult.'';
$remainder -= $mult*$divisor;
}
if (strlen($dividend) > 0 && $dividend*1 == 0) {
$division .= $dividend;
}
return $remainder;
}