Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a single ampersand ('&') operator do in PHP? [duplicate]

$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
print_r(array_filter($array1, "odd"));


function odd($var)
{
    // returns whether the input integer is odd//

    return($var & 1);
}

In the return value what do & operator ?? how it return odd number

like image 575
Jaskaran singh Rajal Avatar asked Mar 13 '14 10:03

Jaskaran singh Rajal


People also ask

What does single ampersand do in C?

The bitwise AND operator is a single ampersand: & . It is just a representation of AND which does its work on the bits of the operands rather than the truth value of the operands. Bitwise binary AND performs logical conjunction (shown in the table above) of the bits in each position of a number in its binary form.

What does a single ampersand mean in C++?

Description. The bitwise AND operator in C++ is a single ampersand & , used between two other integer expressions. Bitwise AND operates on each bit position of the surrounding expressions independently, according to this rule: if both input bits are 1, the resulting output is 1, otherwise the output is 0.

What does single & operator mean?

The single AND operator (&) is known as the Bitwise AND operator. It operates on a single bit. It takes two operands. A bit in the result is 1 if and only if both of the corresponding bits in the operands are 1. The result of the operator may be any number.

What does ampersand do in coding?

The ampersand is the address of operator. It returns the memory location of a variable and that's the only way it's used, prefixed to a variable like the engine on a train. The variable doesn't even have to be initialized, just declared.


1 Answers

It is bitwise operator AND.

For example, ($a & $b) evaluates both $a and $b is turned "on" (i.e. equal to 1)

See this: http://www.php.net/manual/en/language.operators.bitwise.php

like image 181
Raptor Avatar answered Sep 28 '22 18:09

Raptor