Home » Php » What is this PHP syntax with a caret called and what does it do?

What is this PHP syntax with a caret called and what does it do?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I came across this syntax in a codebase and I can’t find any more info on it. It looks like the caret operator (XOR operator), but because the statement below was executed when a certain condition was met I don’t think that is it.

$this->m_flags ^= $flag;

Because I don’t know what it’s called I also can’t search for it properly..

Update:
Because of Cletus’ answer:
Are the following lines then functionally equal?

$a = $a ^ $b; 
$a ^= $b; // the shorthand for the line above
How to&Answers:

It’s bitwise XOR equals. It basically toggles a flag because I’m getting $flag is a power-of-2. To give you an example:

$a = 5; // binary 0101
$b = 4; // binary 0100
$a ^= $b; // now 1, binary 0001

So the third bit has been flipped. Again:

$a ^= $b; // now 5, binary 0101

Answer:

Bitwise XOR and assign operator
http://php.net/manual/en/language.operators.bitwise.php