<?php
function wp_total_pages( $total_items, $per_page ) {
$remainder = $total_items % $per_page;
if ( $remainder ) {
$total_items += $per_page - $remainder;
}
return $total_items / $per_page;
}
$input_sets = [
[506, 10],
[505, 10],
[504, 10],
[500, 9.5],
[500, 9],
[35478743512, 15],
[PHP_INT_MAX, 20],
[PHP_INT_MAX + 250, 20],
];
foreach ($input_sets as list($a, $b)) {
echo PHP_EOL, '=================================================', PHP_EOL;
echo 'Given the following inputs:', PHP_EOL;
var_dump($a, $b);
$over = $a % $b;
$over = ($over > 0) ? 1 : 0;
echo PHP_EOL, 'The results will be:', PHP_EOL;
echo 'ceil: ', var_dump(ceil($a/$b));
echo 'round + ceil: ', var_dump(round(ceil($a/$b)));
echo 'wp_total_pages: ', var_dump(wp_total_pages($a, $b));
try {
if (function_exists('intdiv')) {
echo 'intdiv: ', var_dump($over + intdiv($a, $b));
} else {
echo 'intdiv function not available', PHP_EOL;
}
} catch (Error $e) {
echo 'intdiv: ', $e->getMessage(), PHP_EOL;
}
try {
echo 'bcdiv: ', var_dump($over + bcdiv($a, $b));
} catch (Error $e) {
echo 'bcdiv: ', $e->getMessage(), PHP_EOL;
}
echo PHP_EOL, 'And the results with an int cast added will be:', PHP_EOL;
echo 'ceil: ', var_dump((int) ceil($a/$b));
echo 'round + ceil: ', var_dump((int) round(ceil($a/$b)));
echo 'wp_total_pages: ', var_dump((int) wp_total_pages($a, $b));
try {
if (function_exists('intdiv')) {
echo 'intdiv: ', var_dump((int)($over + intdiv($a, $b)));
} else {
echo 'intdiv function not available', PHP_EOL;
}
} catch (Error $e) {
echo 'intdiv: ', $e->getMessage(), PHP_EOL;
}
try {
echo 'bcdiv: ', var_dump((int) ($over + bcdiv($a, $b)));
} catch (Error $e) {
echo 'bcdiv: ', $e->getMessage(), PHP_EOL;
}
}
preferences:
26.25 ms | 408 KiB | 5 Q