PHP tips: how to replace two variable values without a third variable

Source: Internet
Author: User
PHP skills-solutions to interchange two variable values without third variables

I mentioned the basic methods for exchanging values of two variables in PHP in the Bubble Sort algorithm explanation in PHP. this is because I am mainly talking about Bubble sorting, so I didn't go into details. Now let's take a look at how to exchange two variables in PHP without using the third variable.

Body (For more information, see code comments)

I. substr () & strlen ()

Code:

 '; // Output the original value $. = $ B; // Append the value of $ B to $ a./*** $ B obtains the value of $ a for details: * first use strlen () calculate the length of the string in $ a and $ B respectively. [$ a is the combination of original $ a and $ B.] * use strlen ($ a)-strlen ($ B) the length of the original $ a value is obtained * the length starting from 0 to $ a in the merged $ a using the substr () method, then you can get the original $ a value * $ a and get the $ B value details: * because $ B is already the original value of $ a at the moment, the merged value of $ a is the original value of $ a + original $ B. Therefore, substr () is used to extract the value from the length position of $ B (original $ a) in $, then, if the content is original $ B, the $ B value is paid to $ a for Success */$ B = substr ($ a, 0, (strlen ($) -strlen ($ B); $ a = substr ($ a, strlen ($ B); echo 'value of $ a after Exchange :'. $. ', $ B value :'. $ B ,'
'; // Output result value

Running result:

$ A value before switching: This is A, $ B value: This is B after switching $ a value: This is B, $ B value: This is

II. str_replace ()

Code:

 '; // Output the original value $. = $ B; // Append the value of $ B to $ a $ B = str_replace ($ B, "", $ ); // in $ a (original $ a + $ B), replace $ B with null, and the remaining return value is $ a = str_replace ($ B ,"", $ a); // at this time, $ B is the original $ a value, and $ B (original $ a) is replaced with null in $ a (original $ a + $ B, then the remaining return values are original $ B. The value of $ a after the switch is successful echo :'. $. ', $ B value :'. $ B ,'
'; // Output result value

Running result:

$ A value before switching: This is A, $ B value: This is B after switching $ a value: This is B, $ B value: This is

III. list () & list ()

Code:

 '; // The output original value list ($ B, $ a) = array ($ a, $ B); // The list () function assigns a value to a group of variables with elements in the array. To understand this, I believe you don't need to talk about anything else. echo 'after the exchange, the value of $ a: '. $ a.', the value of $ B: '. $ B ,'
'; // Output result value

Running result:

$ A value before switching: This is A, $ B value: This is B after switching $ a value: This is B, $ B value: This is

IV. exclusive or

Code:

 '; // Output original value/*** original binary: * $ a: 010101000110100001101001011100110010000001101001011100110010000001000001 * $ B: 010101000110100001101001011100110010000001101001011100110010000001000010 ** the following uses bitwise XOR or swapping, for details, refer to the following binary process, */$ a = $ a ^ $ B; // at the moment $ a: 000000000000000000000000000000000000000000000000000000000000000000000011 $ B = $ B ^ $; // at the moment $ B: 010101000110100001101001011100110010000001101001011100110010000001000001 $ a = $ a ^ $ B; // at the moment $ a: 010101000110100001101001011100110010000001101001011100110010000001000010 echo 'the value of $ a after the exchange :'. $. ', $ B value :'. $ B ,'
'; // Output result value

Running result:

$ A value before switching: This is A, $ B value: This is B after switching $ a value: This is B, $ B value: This is

5. plus (+) minus (-) operator

Code:

 '; // Output original value $ a = $ a + $ B; // $ a $ B and value $ B = $ a-$ B; // not explained .. $ a = $ a-$ B; // not explained .. echo 'value of $ a after Exchange :'. $. ', $ B value :'. $ B ,'
'; // Output result value

Running result:

$ A value before switching: 1, $ B value: 2 after switching $ a value: 2, $ B value: 1

Summary

OK, the above is almost all the methods for exchanging two variable values without using the third variable in php. of course, there must be a better one. I am here to discuss it.

In the end, they are all small algorithms. you can study them if you are free.

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.