0

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

Below is the PHP code:

<?php 
    $a = $b['key'] | 0;
?>

Is it an operator?

Community
  • 1
  • 1
Tom
  • 9
  • 1
  • 1
    What does the PHP documentation say? – Oded Sep 01 '11 at 15:25
  • 1
    http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php – Mike Sep 01 '11 at 15:27
  • This is a Bitwise [Inclusive or (OR)](http://en.wikipedia.org/wiki/Inclusive_or) operator. It is a pretty much standard amongst programming languages. See the [manual](http://php.net/manual/en/language.operators.bitwise.php). Whenever you see an operator you don't know, check the manual on operators first. :) – netcoder Sep 01 '11 at 15:29
  • @netcoder No, xor would be `^`. This is a bitwise or ;) – NikiC Sep 01 '11 at 15:30
  • @netcode -- it is a bitwise **inclusive** OR – Naftali Sep 01 '11 at 15:30

6 Answers6

6

The | is the bitwise OR operator.

Doing a bitwise OR with zero (| 0) doesn't make any sense though, as it will not flip any bits. Maybe the guy who wrote this was just a really bad programmer and tried to cast a string to an integer that way. He should have used a (string) cast instead!

NikiC
  • 100,734
  • 37
  • 191
  • 225
  • if `$b['key']` value exists but is empty (null), this construct allow to assign 0 instead of null to $a (might be usefull for databases). – roselan Sep 01 '11 at 15:38
  • @NikiC, tks. What exactly do you mean by " it will not flip any bits"? Say, if $b['key'] is integer '345', what is assigned to $a in this code? is it integer '3450'? this operator looks strange to me.. – Tom Sep 01 '11 at 16:06
  • @Tom: It will be assigned 345. Nothing changes. That's why doing `| 0` is kinda pointless ;) – NikiC Sep 01 '11 at 16:56
1

Its bitwise OR, as defined here.

For example:

 Bitwise Inclusive OR
( 5 = 0101) = ( 0 = 0000) | ( 5 = 0101)
( 5 = 0101) = ( 1 = 0001) | ( 5 = 0101)
( 7 = 0111) = ( 2 = 0010) | ( 5 = 0101)
( 5 = 0101) = ( 4 = 0100) | ( 5 = 0101)
(13 = 1101) = ( 8 = 1000) | ( 5 = 0101)
TJHeuvel
  • 12,403
  • 4
  • 37
  • 46
  • 1
    The OP asked about `|` **not `||` – Naftali Sep 01 '11 at 15:26
  • @TJHeuvel Tks. This is a good example. Now I get the operator. Don't know why it's used in place of `||` though.. since the array should contain an integer. – Tom Sep 01 '11 at 16:13
0

| is a bitwise OR function.

|| is a regular OR

Naftali
  • 144,921
  • 39
  • 244
  • 303
0

It is the bitwise OR operator.

See here for more details:

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

Ian P
  • 12,840
  • 6
  • 48
  • 70
0

It is performing a bitwise OR operation.

It is possible the original author of the code meant to put another | in to make it a logical OR (||), because a bitwise OR with 0 will have no effect whatsoever on the output. Although even this makes no sense, he could have simply cast to an integer to get the same result.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
0

| is a bitwise operator (http://php.net/manual/en/language.operators.bitwise.php)

| means "Bits that are set in either $b['key'] or 0 are set."

Because the second part is a zero though, it will return false only if $b['key'] is zero too.

Matt
  • 9,068
  • 12
  • 64
  • 84