Example: #1 - Swapping of two numbers using temporary variable
PHP Input Screen
<?php
class SwapExample {
public function swap() {
$a = 3;
$b = 6;
echo("The value of a before swap is " . $a . "\n");
echo("The value of b before swap is " . $b . "\n");
// Swapping of two numbers using temp variable
$temp = $a;
$a = $b;
$b = $temp;
echo("The value of a after swap is " . $a . "\n");
echo("The value of b after swap is " . $b . "\n");
}
}
$obj = new SwapExample();
$obj->swap();
PHP Output Screen
The value of a before swap is 3
The value of b before swap is 6
The value of a after swap is 6
The value of b after swap is 3