PHP technique without the third variable swap two variable value solution

Source: Internet
Author: User
Objective

The last time in the article "Php bubble sort (Bubble sort) algorithm in detail" has been mentioned in PHP Exchange two variable value of the basic method, because mainly talk about bubble sort, so did not delve into. So here we are here today to see how, in PHP, it is worthwhile to exchange two variables without using the third variable.

Text (explanation see code comment)

I, substr () && strlen ()

Code:

 
  '; 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, '
'; Output result value

Operation Result:

Value before swapping $a: this is a, the value of $b: The value $a after the interchange of this is B: the value of this is B, the $b: this is a

Second, Str_replace ()

Code:

 
  '; 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, '
'; Output result value

Operation Result:

Value before swapping $a: this is a, the value of $b: The value $a after the interchange of this is B: the value of this is B, the $b: this is a

Iii. list () && list ()

Code:

 
  '; 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, '
'; Output result value

Operation Result:

Value before swapping $a: this is a, the value of $b: The value $a after the interchange of this is B: the value of this is B, the $b: this is a

Iv.. Xor or

Code:

 
  '; 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, '
'; Output result value

Operation Result:

Value before swapping $a: this is a, the value of $b: The value $a after the interchange of this is B: the value of this is B, the $b: this is a

Five, plus (+) minus (-) operators

Code:

 
  '; 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, '
'; 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.