search
HomeDaily ProgrammingPHP KnowledgeSwap two variable values ​​in PHP

PHP has two methods to implement the exchange of two variable values. The first method uses a third temporary variable. The second method is to swap the values ​​of two variables without using the third variable.

Swap two variable values ​​in PHP

Below we will introduce to you the PHP method of exchanging two variable values through specific code examples.

First of all, everyone must know that exchanging two variables means exchanging the values ​​of variables. Typically this is done via data in memory.

Method 1: The easiest way to swap two variables is to use a third temporary variable

<?php
$a = 15;
$b = 27;

// 交换逻辑

echo "<br>交换前的数字为:<br>";
echo "a =".$a." 且 b=".$b;

$temp = $a;
$a = $b;
$b = $temp;

echo "<br>交换后的数字为: <br>";
echo "a =".$a." 且 b=".$b."<br>";

Output:

交换前的数字为:
a =15 且 b=27
交换后的数字为: 
a =27 且 b=15

Method Two : Swap two variable values ​​without using the third variable

<?php
$a = 15;
$b = 27;
echo "<br>交换前:  ". $a . &#39;,&#39; . $b;
list($a, $b) = array($b, $a);
echo "<br>交换后:  ". $a . &#39;,&#39; . $b."<br>";

Output:

交换前: 15,27
交换后: 27,15

Note:

list() function means assigning the values ​​in the array to a set of variables

list ( mixed $var1 [, mixed $... ] ) : array

Like array(), this is not a real function, but a language structure. list() can assign values ​​to a set of variables in a single operation.

Parameter var1 represents a variable. The return value is the specified array.

This article is an introduction to two methods of exchanging two variable values ​​​​in PHP. It is simple and easy to understand. I hope it will be helpful to friends in need!

The above is the detailed content of Swap two variable values ​​in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment