开发者

What does ^ mean in PHP?

开发者 https://www.devze.com 2022-12-28 04:09 出处:网络
I came across this line of code in an application I am revisi开发者_开发百科ng: substr($sometext1 ^ $sometext2, 0, 512);

I came across this line of code in an application I am revisi开发者_开发百科ng:

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

What does the ^ mean?


^ 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


XOR (exclusive OR):

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


It's a bitwise operator.

Example:

"hallo" ^ "hello"

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


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


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号