What does ^ mean in PHP?

前端 未结 7 828
予麋鹿
予麋鹿 2020-12-11 16:45

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

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

What does the ^ mean

相关标签:
7条回答
  • 2020-12-11 17:13

    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 讨论(0)
  • 2020-12-11 17:17

    XOR (exclusive OR):

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

    0 讨论(0)
  • 2020-12-11 17:22

    It's a bitwise operator.

    Example:

    "hallo" ^ "hello"
    

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

    0 讨论(0)
  • 2020-12-11 17:24

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

    0 讨论(0)
  • 2020-12-11 17:29

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

    0 讨论(0)
  • 2020-12-11 17:30

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

    0 讨论(0)
提交回复
热议问题