mahen3d mahen3d - 3 months ago 25
PHP Question

Php7 Upgrade issues with << bitwise operators

I had following code working in php 5.6 but after php7 upgrade, it stops working, do you have any idea how to do this on PHP 7.0 ?

public function getChecksParam($array) {
$v = 0;
foreach ( $array as $param ) {
$v |= 1 << $param - 1;
}
return $v;
}

Answer

Issue was causing after the upgrade PHP seems to have fixed a bug that when it defaults to 0 on invalid bitwise-shift

so i have to do some validations before doing the bitwise shift see below for the correct code that working.

public function getChecksParam($array) {
    $v = 0;
    foreach ( $array as $param ) {
         if($param > 0 )
           $v |= 1 << $param - 1;
         else
           $v = 0;   
    }
    return $v;
}
Comments