8
votes

I came across this line of code in an application I am revising:

substr($sometext1 ^ $sometext2, 0, 512);

What does the ^ mean?

7

7 Answers

6
votes

It's a bitwise operator.

Example:

"hallo" ^ "hello"

It outputs the ASCII values #0 #4 #0 #0 #0 ('a' ^ 'e' = #4).

8
votes

^ is the bitwise exclusive OR operator. For each bit in a value, it looks to see if that bit is the same in the other value; if it is the same, a 0 is output in its place, otherwise a 1 is output. For example:

  00001111
^ 01010101
  --------
  01011010
7
votes

XOR (exclusive OR):

$a ^ $b means bits that are set in $a or $b, but not both, are set.

2
votes

That's the bitwise OR operator - in PHP, it also applies to strings.

2
votes

In PHP, ^ means 'bitwise XOR'. Your code XORs together two strings, then returns at most the first 512 characters.

In other words it does this:

return (at most the first 512 characters of (someText1 XOR someText2))
0
votes

^ matches the starting position within the string. In line-based tools, it matches the starting position of any line.