Understanding PHP & (Ampersand, Bitwise And) Operator

Understanding PHP & (ampersand, bitwise and) operator

& is binary and. If you have a binary value, and you and with another binary value, then the result will be the bitwise and of the two. An example:

  01101010
& 01011001
= 01001000

The rightmost bit is either a 1 (and in that case the number is an odd number) or it is a 0, in which case the number is even. If you & a number with 1, you only look at the least significant bit, and the if checks if the number is a 1 or a 0. As others have mentioned, look at the bitwise operators for info on how they work.

What do the =& and &= operators in PHP mean?

$a &= $b is short for $a = $a & $b which is the bitwise-and operator.

$a =& $b assigns $a as a reference to $b.

& operator in a conditional statement

It's the PHP bitwise AND operator: http://php.net/manual/en/language.operators.bitwise.php

What does a single ampersand mean in a PHP conditional statement?

That is a bitwise AND operation: http://www.php.net/manual/en/language.operators.bitwise.php

If, after the bitwise AND, the result is "truthy", the clause will be satisfied.

For example:

3 & 2 == 2 // because, in base 2, 3 is 011 and 2 is 010
4 & 1 == 0 // because, in base 2, 4 is 100 and 1 is 001

This is commonly used to check a single bit in a bitset, by testing powers of two, you are actually checking if a specific bit is set.

How bitwise operator works

Odd numbers in binary always have a least-significant bit (LSB) of 1. That is why your code

function odd($var){
return ($var & 1);
}

returns true on odd numbers. Here are your examples from your question:

(decimal) 4 & 1 = (binary) 100 & 001 = (binary) 000 = (decimal) 0 = false
(decimal) 5 & 1 = (binary) 101 & 001 = (binary) 001 = (decimal) 1 = true

Another way to think of it is

    100 (decimal 4) - an even number 
AND 001 (decimal 1)
= 000 (decimal 0) - return false

and

    101 (decimal 5) - an odd number  
AND 001 (decimal 1)
= 001 (decimal 1) - return true

Difference between & and && in PHP

& is bitwise AND. See Bitwise Operators. Assuming you do 14 & 7:

    14 = 1110
7 = 0111
---------
14 & 7 = 0110 = 6

&& is logical AND. See Logical Operators. Consider this truth table:

 $a     $b     $a && $b
false false false
false true false
true false false
true true true

PHP & operator

Those are references, and they are similar to "pointers" in C or C++.

More information is in the PHP manual.

In this case, since $db_hosts is empty, the construct $db_hosts[$db_item['hostid']] will create a new array with an empty item on the index of $db_item['hostid'] and return the item's reference, making $db_host act as an 'alias' for whatever $db_hosts[$db_item['hostid']] is currently.

What is the effect of a double negating bitwise operator (~~) - also called double tilde - in PHP?

It should be !! (it converts the value to a boolean) but it is not needed at all. I guess the original coder mistaken ~ for ! then they added (bool) in front of it to achieve the desired result (because, as you noticed in the question, ~~ is a no-op).

The ternary operator (?:) forces the evaluation of its first argument as boolean.

The boolean value of $field->req is the same as of !! $field->req and (bool) ~~$field->req (and (bool)$field->req btw).

I would remove the (bool) ~~ part completely to get smaller and cleaner code.

Edit by questioner: The only effect of ~~ in PHP is to cut of decimals from a float value.

See the following results:

$a = 2.123;
$b = -2.123;
$c = new stdClass();
$d = ["a",2,"c"];
$e = "lord";
$f = -3;
$g = false;
$h = null;
$j = -2.99;
$k = 2.99;

var_dump(~~$a);
var_dump(~~$b);
// var_dump(~~$c); // error
// var_dump(~~$d); // error
var_dump(~~$e);
var_dump(~~$f);
// var_dump(~~$g); // error
// var_dump(~~$h); // error
var_dump(~~$j);
var_dump(~~$k);

var_dump(!!$a);
var_dump(!!$b);
var_dump(!!$c);
var_dump(!!$d);
var_dump(!!$e);
var_dump(!!$f);
var_dump(!!$g);
var_dump(!!$h);
var_dump(!!$j);
var_dump(!!$k);

int(2) int(-2) string(4) "lord" int(-3) int(-2) int(2)
bool(true) bool(true) bool(true) bool(true) bool(true) bool(true)
bool(false) bool(false) bool(true) bool(true)



Related Topics



Leave a reply



Submit