Swap Two number Program in PHP
Swap two number with each other means interchange their value. Suppose you have two number a and b a contain 6 and b contain value 5 after swap their value, a have 5 and b have 6.
basically 2 method of interchange value
a-using third variable,
b-without using third variable(Using Arithmetic Operators),
c-usign multiplication,
d-bitwaise XOR operation,
a-swap two number using third
<?php
$a=10;
$b=20;
echo "Value of a: $a</br>";
echo "Value of b: $b</br>";
$a=$a+$b;
$b=$a-$b;
$a=$a-$b;
echo "Value of a: $a</br>";
echo "Value of b: $b</br>";
?>
b-without using third variable(Using Arithmetic Operators),
wap two number with each other means interchange their value. Suppose you have two number a and b a contain 10 and b contain value 20 after swap their value, a have 20 and b have 10.
<?php
$a=10;
$b=20;
echo "Value of a: $a</br>";
echo "Value of b: $b</br>";
$temp=$a;
$a=$b;
$b=$temp;
echo "Value of a: $a</br>";
echo "Value of b: $b</br>";
?>
c-usign multiplication,
Multiplication and division can also be used for swapping.
<?php
$x = 10, $y = 5;
// Code to swap 'x' and 'y'
$x = $x * $y; // x now becomes 50
$y = $x / $y; // y becomes 10
$x = $x / $y; // x becomes 5
printf("After Swapping: $x, $y", $x, $y);
?>
d-bitwaise XOR operation,
The bitwise XOR operator can be used to swap two variables. The XOR of two numbers x and y returns a number which has all the bits as 1 wherever bits of x and y differ.
<?php
int $x = 10, $y = 5;
// Code to swap 'x' (1010) and 'y' (0101)
$x = $x ^ $y; // x now becomes 15 (1111)
$y = $x ^ $y; // y becomes 10 (1010)
$x = $x ^ $y; // x becomes 5 (0101)
print_r("After Swapping: $x, $y ", $x, $y);
?>
list() :
list — Assign variables as if they were an array
<?php
$info = array('coffee', 'brown', 'caffeine');
// Listing all the variables
list($drink, $color, $power) = $info;
?>
Swap two number with each other means interchange their value. Suppose you have two number a and b a contain 6 and b contain value 5 after swap their value, a have 5 and b have 6.
basically 2 method of interchange value
a-using third variable,
b-without using third variable(Using Arithmetic Operators),
c-usign multiplication,
d-bitwaise XOR operation,
a-swap two number using third
<?php
$a=10;
$b=20;
echo "Value of a: $a</br>";
echo "Value of b: $b</br>";
$a=$a+$b;
$b=$a-$b;
$a=$a-$b;
echo "Value of a: $a</br>";
echo "Value of b: $b</br>";
?>
b-without using third variable(Using Arithmetic Operators),
wap two number with each other means interchange their value. Suppose you have two number a and b a contain 10 and b contain value 20 after swap their value, a have 20 and b have 10.
<?php
$a=10;
$b=20;
echo "Value of a: $a</br>";
echo "Value of b: $b</br>";
$temp=$a;
$a=$b;
$b=$temp;
echo "Value of a: $a</br>";
echo "Value of b: $b</br>";
?>
c-usign multiplication,
Multiplication and division can also be used for swapping.
<?php
$x = 10, $y = 5;
// Code to swap 'x' and 'y'
$x = $x * $y; // x now becomes 50
$y = $x / $y; // y becomes 10
$x = $x / $y; // x becomes 5
printf("After Swapping: $x, $y", $x, $y);
?>
d-bitwaise XOR operation,
The bitwise XOR operator can be used to swap two variables. The XOR of two numbers x and y returns a number which has all the bits as 1 wherever bits of x and y differ.
<?php
int $x = 10, $y = 5;
// Code to swap 'x' (1010) and 'y' (0101)
$x = $x ^ $y; // x now becomes 15 (1111)
$y = $x ^ $y; // y becomes 10 (1010)
$x = $x ^ $y; // x becomes 5 (0101)
print_r("After Swapping: $x, $y ", $x, $y);
?>
list() :
list — Assign variables as if they were an array
<?php
$info = array('coffee', 'brown', 'caffeine');
// Listing all the variables
list($drink, $color, $power) = $info;
?>
Comments