chicane
chicane

Reputation: 2081

What does ^ mean in PHP?

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

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

What does the ^ mean?

Upvotes: 8

Views: 4122

Answers (7)

huh
huh

Reputation: 1

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

Upvotes: 0

Cam
Cam

Reputation: 15234

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))

Upvotes: 2

Mitch Dempsey
Mitch Dempsey

Reputation: 39929

XOR (exclusive OR):

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

Upvotes: 7

Darin Dimitrov
Darin Dimitrov

Reputation: 1039200

It's a bitwise operator.

Example:

"hallo" ^ "hello"

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

Upvotes: 6

mipadi
mipadi

Reputation: 411012

^ 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

Upvotes: 8

Michael Borgwardt
Michael Borgwardt

Reputation: 346407

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

Upvotes: 2

Daniel DiPaolo
Daniel DiPaolo

Reputation: 56408

It's the XOR (exclusive-or) operator. For strings it's used as simple encryption.

Upvotes: 2

Related Questions