Add two numbers in PHP

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.