tags:

views:

100

answers:

4

I wonder what % and & in the following PHP code do.

$num_one = rand() % 10;
$num_two = rand() & 10;

Thanks in advance.

+3  A: 

% is modulus (remainder after division).

& is bitwise and.

geofftnz
+1  A: 

& is the bitwise AND operator. % is the modulus (remainder of rand() / 10 in this case).

eyelidlessness
+1  A: 

& is a bitwise operator. click me

% is an arithmetic operator. click me

Ahmet Kakıcı
+5  A: 

% is modulo, so

$num_one = rand() % 10

gives you a number between in [0..10), while & means bitwise and, such that

$num_one = rand() & 10

gives you a random number with only a combination of the bits 2 and 4 set, like in [2,8,10].

Ralph Rickenbach