Why does the output of this code look like this?

20 Views Asked by At

In PHP language,

$var = ("!"^"@");

Why does the value of the line come out as "a?

Demo - https://3v4l.org/b0saN

1

There are 1 best solutions below

1
Phil On

See Bitwise Operators

If both operands for the &, | and ^ operators are strings, then the operation will be performed on the ASCII values of the characters that make up the strings and the result will be a string

"!" -> ASCII 33 -> 0b0100001
"@" -> ASCII 64 -> 0b1000000

    0100001
XOR 1000000
===========
    1100001 = ASCII 97 = "a"