What "|" character do? [duplicate]
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;
?>
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";
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.
|
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
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
精彩评论