Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

GMP Shift Operators Validate A GMP Right Operand

The GMP shift operators (<< and >>) accept a right operand outside the range of a regular PHP integer when it is itself a GMP object. Until PHP 8.6, a right operand greater than the platform’s unsigned long maximum was silently truncated, producing an incorrect shift amount. In PHP 8.6, a right operand outside the unsigned long range throws a ValueError.

PHP code

<?php

$a = gmp_init(2);
$huge = gmp_init('18446744073709551616');

try {
    var_dump(gmp_strval($a << $huge));
} catch (\ValueError $e) {
    echo "ValueError: ".$e->getMessage()."\n";
}

?>

Before

string(1) "2" 

After

ValueError: Shift must be between 0 and 18446744073709551615

PHP version change

This behavior changed in 8.6.

See Also

Error Messages

Extension

© 2023-2026, Damien Seguy - Exakat