How does the XOR trick to swap two variables really work on a string?

前端 未结 1 551
慢半拍i
慢半拍i 2021-01-20 00:57

I just ran across this snippet of code for swapping the values of two variables in PHP:



        
相关标签:
1条回答
  • 2021-01-20 01:33

    PHP applies bitwise operators to strings by applying it to each character individually.

    PHP: Bitwise Operators:

    Be aware of data type conversions. If both the left-hand and right-hand parameters are strings, the bitwise operator will operate on the characters' ASCII values.

    This will work if both strings have the same number of characters, or more precisely the same number of bytes. If the above quote is really precise, then it may only work for ASCII-only strings.

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