Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to exchange the values of two variables without the third variable in PHP

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to achieve PHP without the third variable to exchange the value of two variables, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

I have done a php interview question that goes like this: do not use the third variable to exchange the values of two variables. Generally speaking, the value exchange of the original two variables is realized with the help of the third intermediate variable, but this problem requires that the intermediate variable can not be used, which is also a difficult problem for beginners. Several methods found on the Internet are summarized as follows:

The code is as follows:

/ / string version is implemented by combining the two methods of substr,strlen

$a = "a"

$b = "b"

Echo 'before the exchange of $aVOB. Recording. Recording.

$a.roomroomb

$b=substr ($a) 0, (strlen ($a)-strlen ($b))

$a=substr ($a, strlen ($b))

After the exchange of echo', $aVOB is exchanged.change.recording.

Echo'-'

/ / the string version is implemented using the str_replace method

$a = "a"

$b = "b"

Echo 'before the exchange of $aVOB. Recording. Recording.

$a.roomroomb

$b=str_replace ($b, "", $a)

$a=str_replace ($b, "", $a)

After the exchange of echo', $aVOB is exchanged.change.recording.

Echo'-'

/ / string version is implemented by combining list method with array

$a = "a"

$b = "b"

Echo 'before the exchange of $aVOB. Recording. Recording.

List ($bdjingha) = array ($arecoveryb)

After the exchange of echo', $aVOB is exchanged.change.recording.

Echo'-'

/ / both strings and numbers are suitable for XOR operations

$axianga'

$breadcrumb

Echo 'before the exchange of $aVOB. Recording. Recording.

$a million $a ^ $b

$baked $b^ $a

$a million $a ^ $b

After the exchange of echo', $aVOB is exchanged.change.recording.

Echo'-'

/ / for numbers only

$axi3

$bread5

Echo 'before the exchange of $aVOB. Recording. Recording.

$a=$a+$b

$b=$a-$b

$a=$a-$b

After the exchange of echo', $aVOB is exchanged.change.recording.

The above is all the contents of the article "how to exchange the values of two variables without the third variable in PHP". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report