I wanted to update the PHP socket Code But there is really no mood for project reasons, but after a while, the SMS gateway will be operated, and then the socket code will naturally be updated. Today we will mainly discuss how to use PHP to exchange two numbers.
First, use the PHP list data structure. On the code, and then parse
Function swap (& $ A, & $ B ){List ($ A, $ B) = array ($ B, $ );}
List: Assign the values in the array to the variables in the list, which is equivalent to assigning the value of $ B to $ A and assigning the value of $ A to $ B. at the same time, pay attention to the use of the reference operator, here is the reference transfer rather than the value transfer.
Second, we should first popularize the basic knowledge of exclusive or operation. XOR operator: ^. A simple understanding is: the same is false, the difference is true
Three Characteristics of bitwise XOR:
1.0 ^ 1 = 1 0 ^ 0 = 0 => therefore, 0 is different or any number is equal to any number.
2.1 ^ 0 = 1 1 ^ 1 = 0 => therefore, 1 is different or any number is equal to any number. if you set yourself to 0 at the same time, the exchange rate and the combination rate are normal.
Function swap1 (& $ A, & $ B ){$ A = $ A ^ $ B;$ B = $ A ^ $ B;$ A = $ B ^ $;}
Okay. Let's take a look at why the exchange can be realized.
$ B = $ A ^ $ B = ($ A ^ $ B) ^ $ B = $ A ^ ($ B ^ $ B) = $ A ^ 0 based on differences or features, 0 is different from any number or is equal to any number. Similarly, it can be pushed down. $ A = $ B
I personally prefer to use the first method because it is concise and refreshing.