What is the means of the single '|' operator in PHP?

Discussion in 'PHP' started by Kaan Yılmaz, Mar 28, 2020.

  1. #1
    Hi there!
    What is the means of the single '|' operator in PHP? How can I use this operator? I looked at the php.net, but I couldn't learn info about this operator.

    I asking the SINGLE (|) operator, NOT THE DOUBLE (||) operator.

    Thanks.
     
    Kaan Yılmaz, Mar 28, 2020 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    binary comparison instead of boolean. Same applies to & vs. &&.

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

    bitwise if either is true it's true (1), otherwise it's false.

    0b0001 | 0b0001 == 0b0001
    0b0001 | 0b0000 == 0b0001
    0b0000 | 0b0000 == 0b0000
    0b0010 | 0b0001 == 0b0011 (3 decimal)

    Binary and, bitwise both have to be true (1) for a true result, otherwise false (zero)

    0b0001 & 0b0001 == 0b0001
    0b0001 & 0b0000 == 0b0000
    0b0000 & 0b0000 == 0b0000
    0b0010 & 0b0001 == 0b0000

    There's also xor, where if both are true (1), the result is false (0), otherwise it behaves like single |

    0b0001 ^ 0b0001 == 0b0000
    0b0001 ^ 0b0000 == 0b0001
    0b0000 ^ 0b0000 == 0b0000
    0b0010 ^ 0b0001 == 0b0011

    XOR is used in bitmap graphics a lot, since it's an easy way to draw a inverse video sprite, that can be undrawn by simply xor'ing the sprite onto the back-buffer.

    If you're not familiar with binary operations, this probably doesn't make much sense.
     
    deathshadow, Mar 28, 2020 IP
    Saputnik and JEET like this.
  3. Kaan Yılmaz

    Kaan Yılmaz Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Wow man, thank you very much for answering.
     
    Kaan Yılmaz, Mar 28, 2020 IP