-2

I'm reading other's php code, and there's one line writes:

    if ($isprivate)
  {
    $status |= STATUS_PRIVATE;
  }

I have no idea what does "|=" mean? Can anyone help on it?

user1453951
  • 185
  • 1
  • 7
  • 16

2 Answers2

4

| is a bitwise operator. It takes two numbers and conducts a bitwise OR operation. http://en.wikipedia.org/wiki/Bitwise_OR#OR

Ex: A = 4 B = 3

In binary: A = 100 B = 011

A | B == 111 (in binary) == 7 (in decimal)

A |= B is the same as A = A | B

In your specific example, the code is checking to see if $isPrivate is true. If it is, it makes the bit marked by STATUS_PRIVATE set to TRUE in the $status variable.

Fiarr
  • 858
  • 5
  • 16
1

This is a shorthand of this:

$status =  $status | STATUS_PRIVATE;

Or (inclusive or)

For example

define(STATUS_PRIVATE, 0b01);
$status = 0b00;
$status |= STATUS_PRIVATE; //status become 0b01;
sectus
  • 15,605
  • 5
  • 55
  • 97