PHP technique without the third variable swap two variable value solution

Source: Internet
Author: User

Text (explanation see code comment)

I, substr () && strlen ()

Code:

<?php/** * When a variable is a string, the available Exchange method one * uses substr () combined with strlen () two methods to achieve the exchange variable worth the purpose */$a = "This is a"; A variable original value $b = "this is B"; b Variable original value echo ' Exchange before $a value: '. $a. ', $b value: '. $b, ' <br> '; Output original Value $ A. = $b; Append the value of $b to $ A/** * $b get a $ A value in detail: * The      length of the string in $ A and $b is calculated first by strlen () respectively "At this time $ A is the value of the original $ A and $b" *      through strlen ($a)-strlen ($b) You can get the value length of the original $ A *      in the merged $ A by substr () method from 0 to the length of a $ A, then you can obtain the original $ A value * $a get the $b value in detail: *      because the $b is already a $ A original value, and $ A combined value of the original $ A + original $b value, so the substr () in $ A from the $b (original $ A) length position interception, the content is the original $b, then the $b value to $ A success  /$b = substr ($a, 0, (strlen ($a)-strlen ($b)); A  = substr ($a, strlen ($b)); $a value after Echo ' Interchange: '. $a. ', $b value: '. $b, ' <br> ';//output result value

Operation Result:

Value $a before swapping: this is a, $b value: this is b
Value after Exchange $a: This is B, the value of $b: This is a

Second, Str_replace ()

Code:

<?php/** * When both variables are strings, the available Exchange Method II * uses the Str_replace () method to achieve the swap variable worth the purpose * This method is slightly simpler than the first one, a little more logically */$a = "This is a"; A variable original value $b = "this is B"; b Variable original value echo ' Exchange before $a value: '. $a. ', $b value: '. $b, ' <br> '; Output original Value $ A. = $b; Append the value of $b to $ A $b  = Str_replace ($b, "", $a); In $ A (original $a+ $b), replace $b with empty, then the remaining return value is $a$a  = Str_replace ($b, "", $a); At this point, the $b is the original $ A value, and the $b (original $ A) is replaced with a null in $ A (original $a+ $b), the remaining return value is the original $b, the value of the exchange succeeds after the Echo ' Interchange $a: '. $a. ', $b value: '. $b, ' <br> '; Output result value

Operation Result:

Value $a before swapping: this is a, $b value: this is b
Value after Exchange $a: This is B, the value of $b: This is a

Iii. list () && list ()

Code:

<?php/** * When a variable is a string, the Exchange method can be used three * using the list () and array () methods to achieve the exchange variable worth the purpose * This method is the first to second, the code is the most concise */$a = "This is a"; A variable original value $b = "this is B"; b Variable original value echo ' Exchange before $a value: '. $a. ', $b value: '. $b, ' <br> '; Output raw Value list ($b, $a) = Array ($a, $b); The list () function assigns values to a set of variables using the elements in the array. Know this, believe the others don't have to say it. Echo ' Exchange $a value after: '. $a. ', $b value: '. $b, ' <br> '; Output result value

Operation Result:

Value $a before swapping: this is a, $b value: this is b
Value after Exchange $a: This is B, the value of $b: This is a

Iv.. Xor or

Code:

<?php/** * When a variable is a string or a number, the available method of Exchange four * uses the XOR operation */$a = "This is a"; A variable original value $b = "this is B"; b Variable original value echo ' Exchange before $a value: '. $a. ', $b value: '. $b, ' <br> '; Output Original Value/** * Original binary: * $a: 010101000110100001101001011100110010000001101001011100110010000001000001 * $b: 0 10101000110100001101001011100110010000001101001011100110010000001000010 *  * The following are mainly used in the bitwise XOR or exchange, please refer to the following binary procedure, */$ A= $a ^ $b; At this moment $a:000000000000000000000000000000000000000000000000000000000000000000000011$b= $b ^ $a; At this moment $b:010101000110100001101001011100110010000001101001011100110010000001000001$a= $a ^ $b; At this moment $a:010101000110100001101001011100110010000001101001011100110010000001000010echo ' Exchange $a value: '. $a. ', $b value: '. $b, ' <br> '; Output result value

Operation Result:

Value $a before swapping: this is a, $b value: this is b
Value after Exchange $a: This is B, the value of $b: This is a

Five, plus (+) minus (-) operators

Code:

<?php/** * When the two variables are numeric, the available Exchange method is five * using the Add and subtract operator, which is equivalent to the mathematical operation of ^_^ */$a = "This is a"; A variable original value $b = "this is B"; b Variable original value echo ' Exchange before $a value: '. $a. ', $b value: '. $b, ' <br> '; Output original value $a= $a + $b; $a $b and value $b= $a-$b; Not explained. $a = $a-$b; Not explained. Echo ' Exchange $a after the value: '. $a. ', $b value: '. $b, ' <br> '; Output result value

Operation Result:

Value $a before swapping: 1,

$b value: 2 after Exchange

$a value: 2, $b value: 1

Summarize

OK, the above is almost all the methods in PHP without the use of the third variable to exchange two variable values, of course, there must be better, I am here to be a point.

In the final analysis, all are small algorithms, we can also study under the time.

  • 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.