首页 > 解决方案 > PHP && 给出错误答案

问题描述

我正在将一些 VB 代码转换为 PHP,并且在一行中有一个逻辑运算符 AND。

PHP 和 VB 给了我不同的结果,所以分解每一步,它归结为逻辑运算符 AND,或者 &&,就像它在 PHP 中一样。

在 PHP 中,我有:

($intAscii && $a)

$intAscii = 87具有和的值$a = 128

上面的逻辑应该返回 0,但 PHP 返回 1。

将这些转换为二进制:

    87 = ‭01010111‬
    128 = 10000000‬

所以很明显这应该返回0,那么为什么PHP坚持要返回1呢?

完整功能在这里:

    Function IsBitSet($intAscii , $intBit) {

        //'change the value of the integer passed by
        //'turning on the bit passed
        $a=(int)(pow(2 ,(int)$intBit));
        $b=($intAscii && $a);
        echo '</br>Manual AND  87 && 128 '.(87 && 128) .'</br>';
        echo 'is bit set $intAscii='.$intAscii .' $a='.$a.' And = '.( $b).'</br>';

        Return(($intAscii && $a) <> 0);

    }

和输出:

Manual AND 87 && 128 1
is bit set $intAscii=84 $a=128 And = 1

标签: phplogicphp-7

解决方案


推荐阅读