What is the significance of ‘^’ in PHP?


The ‘^’ is a bitwise operator in PHP, i.e XOR (exclusive OR) bitwise operator, that is used to display the ASCII values of variables in question. For example − For evert bit in the value, the ^ operator checks if that bit is the same in the other value too. If the values are same, 0 is produced as an output, otherwise 1 is shown as output. Below is an example illustrating the same −

Example

 Live Demo

<?php
   $x = "a";
   $y = "b";
   $x ^= $y;
   $y ^= $x;
   $x ^= $y;
   echo $x;
   echo "
";    echo $y; ?>

Output

b
a

Different variables are assigned character values and the ‘^’ operator is used to perform XOR on the two variables. Relevant output is displayed on the console.

Updated on: 01-Jul-2020

169 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements