'Why does the output of this code look like this?

In PHP language,

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

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

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



Solution 1:[1]

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"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Phil