Bitwise not "~" in PHP not working, "Fatal error: Unsupported operand types in ..."
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 ?
精彩评论