Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the methods of exchanging two integer variables with PHP?

Shulou Source: shulou.com Published: 2022-06-02 14:49:08 10月05日 Update

This article mainly introduces "what are the methods for PHP to exchange two integer variables". In daily operation, I believe that many people have doubts about the method of exchanging two integer variables in PHP. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for everyone to answer the doubt that "PHP realizes the method of exchanging two integer variables". Next, please follow the editor to study!

Swap two integer variables

Use an intermediate variable

This is the easiest to understand.

$a = 1, b = 2, temp;var_dump ($a, $b)

Do not use intermediate variables, just rely on a few clever conversions of addition and subtraction

$a = 10 * b = 5 * a = $a + $b * b = $a-$b * a = $a-$b *

Use multiple XOR in bit operations

This is the most difficult to understand.

$a = 1b / s / b = 3x / s / s a = $a ^ $b / s / s / b = $a ^ $b / s / b)

Use the list structure

Note that list () is a structure similar to array ().

This is put forward by qufo users in the comments. Thank you very much.

When using list, pay attention to the php version

$a = 4; $b = 5X list ($b, $a) = [$a, $b]; / / equivalent to [$b, $a] = [$a, $b]; var_dump ($a, $b). At this point, the study on "what is the way for PHP to exchange two integer variables" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Variables two methods learning more structure help practical ingenious next bad articles versions theories users knowledge articles websites materials follow Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Information macOS Apple Redmi