-4

Difference between “+”, “-” and “^” operators in PHP?

echo "<br>";
echo 200+233; //433
echo "<br>";
echo 200^233; //33
echo "<br>";
echo 233^20; //253

As you see sometimes '^' works as '-' and sometimes as '+'...

What a rule?

  • 1
    `^` is xor. `+` is addition and `-` is subtraction. – Wreigh Jul 02 '18 at 03:01
  • 1
    http://php.net/manual/en/language.operators.arithmetic.php vs http://php.net/manual/en/language.operators.bitwise.php – AbraCadaver Jul 02 '18 at 03:04
  • 2
    It would definitely be worth checking out and favoriting https://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php – WNP Jul 02 '18 at 03:08
  • Simply doing a Google search for [php caret](https://www.google.com/search?q=php+caret) tells you exactly what it would do. I suggest you read documentation for the languages you are trying to use. – Matt Clark Jul 02 '18 at 03:24

1 Answers1

1

This is the XOR operator. It's a binary operator that returns true if both inputs are not the same:

0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0

So it's looking at the two numbers you have input as binary numbers and comparing each bit and returning a new result:

200 in binary = 11001000
233 in binary = 11101001
result          00100001

That result as a decimal number is 33.

profexorgeek
  • 1,190
  • 11
  • 21