0

Possible Duplicate:
Reference - What does this symbol mean in PHP?

I googled much for finding answer but i couldn't find any. I saw in few free source codes that they use "|" sign to combine values or something. can someone enlighten what is different between "|" and "+"?

sample:

<?php
define('PERMISSION_DENIED', 0);
define('PERMISSION_READ', 1);
define('PERMISSION_ADD',  2);
define('PERMISSION_UPDATE', 4);
define('PERMISSION_DELETE', 8);


$read_only = PERMISSION_READ;
$read_delete = PERMISSION_READ | PERMISSION_DELETE;
$full_rights = PERMISSION_DENIED | PERMISSION_READ | PERMISSION_ADD | PERMISSION_UPDATE | PERMISSION_DELETE;


$myrights = PERMISSION_READ;
$myrights |= PERMISSION_UPDATE; 
?>

why not just:

<?php
define('PERMISSION_DENIED', 0);
define('PERMISSION_READ', 1);
define('PERMISSION_ADD',  2);
define('PERMISSION_UPDATE', 4);
define('PERMISSION_DELETE', 8);


$read_only = PERMISSION_READ;
$read_delete = PERMISSION_READ + PERMISSION_DELETE;
$full_rights = PERMISSION_DENIED + PERMISSION_READ + PERMISSION_ADD + PERMISSION_UPDATE + PERMISSION_DELETE;


$myrights = PERMISSION_READ;
$myrights += PERMISSION_UPDATE; 
?>
Community
  • 1
  • 1
Shadow Walker
  • 295
  • 1
  • 3
  • 11
  • possible duplicate of [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) - see http://stackoverflow.com/questions/2233835/what-is-the-difference-between-the-and-operators – hakre Sep 30 '11 at 10:26
  • [Bitwise OR operator.](http://en.wikipedia.org/wiki/Bitwise_operation#OR) – Niet the Dark Absol Sep 30 '11 at 10:24

4 Answers4

4

it's the bitwise or operator, so you're operating with numbers in base 2 notation.

for example:

1000 | 0001 = 1001 ==> (8 | 1 = 9)

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

In your code each permission is represented by one bit in a different position, I mean:

1 = 0001 = perm1
2 = 0010 = perm2
4 = 0100 = perm3
8 = 1000 = perm4

so doing or with those numbers gives you the number with all the permissions together. then if you wanna check if a permission is set you gotta do an and operation with the permission that you're checking, for example:

$user_perms = perm1 | perm3;
if ($user_perms & perm4) echo "user does not have this permission";
Packet Tracer
  • 3,794
  • 3
  • 21
  • 35
  • 1
    There is no such thing as "binary numbers", just a binary notation of a number, which is just fancy for base 2 notation. Conflating notation and concept, although appealing to beginners, imho prevents programmers from fully understanding the concepts at hand. – phihag Sep 30 '11 at 10:29
  • you're right, just notice that not everyone here speaks native english and it's not so easy to explain things – Packet Tracer Sep 30 '11 at 10:36
4

Okay, there is a difference. In order to see the difference you can just see how these numbers appear in binary:

0 =  00000000
1 =  00000001
2 =  00000010
4 =  00000100
8 =  00001000

As you can see, each of these numbers have only one bit set to one each in a different position.

Now, having a bitwise OR between them will make the result with one on each position as the operands:

00000010 |
00000100
----------
00000110

In this case it's the same as just adding the numbers.

0 | 0 = 0; 0 + 0 = 0
0 | 1 = 1 | 0 = 1 + 0 = 1

The difference comes here:

1 | 1 = 1

while

1 + 1 = 10 !!

The difference in this example is that bit-wise operator is faster because you just operate on the bits.

Dan Bizdadea
  • 1,264
  • 8
  • 13
3

| is the bitwise OR (every bit of the result is 1 iff at least one of the corresponding bits in the inputs is 1). Using addition would work as well, but doesn't make it clear one is assembling a bitmask, and can lead to bugs, because

PERMISSION_READ + PERMISSION_READ != PERMISSION_READ

but

PERMISSION_READ | PERMISSION_READ == PERMISSION_READ
phihag
  • 245,801
  • 63
  • 407
  • 443
1

This is a bitwise OR operator.

You can find more information about it here : http://php.net/manual/en/language.operators.bitwise.php

Bitwise operators usual are good for soft encription methods

KodeFor.Me
  • 11,719
  • 19
  • 82
  • 154