开发者

Bitwise not "~" in PHP not working, "Fatal error: Unsupported operand types in ..."

开发者 https://www.devze.com 2023-02-08 22:52 出处:网络
PHP Doc (http://php.net/manual/en/language.operators.bitwise.php) clearly states: ~ $aNotBits that are set in $a are not set, and vice versa.

PHP Doc (http://php.net/manual/en/language.operators.bitwise.php) clearly states:

~ $a    Not  Bits that are set in $a are not set, and vice versa.
开发者_如何学Go

So why am I getting a fatal error when trying to run the following line?

~ $noResults;

TIA


You are getting this error because $noResults is not an integer (or string, but that's just because of implicit typecasting).


~ will work on numbers and strings, couldn't get it working on objects and arrays.

AND the variable to negate MUST be set.

 echo ~ $a;  // won't work

whereas

$a = '';
echo ~ $a;  // will work

and

$a; echo ~$a; // will work too.


I have no problems with it :

$a = 5;
$a = ~ $a;
echo $a; // -6

Are you sure $noResults is an integer ?

0

精彩评论

暂无评论...
验证码 换一张
取 消