Swapping two variables without using third one.

It was an interesting way to swapping the values of two variable.There are several ways to do that.I just tried two of the techniques using php5 that was like as belows:

Created a file called class.Swap.php .

It contained the following codes:

class Swap{
    private $a = 2;
    private $b = 5;
    private $x = 13;
    private $y = 3;
    public function before_swap(){
        echo '<h2>The values Before Swap</h2>';
        echo '<br>';
        echo " Value of A = ".$this->a;
        echo '<br>';
        echo " Value of B = ".$this->b;
        echo '<br>';
        echo " Value of X = ".$this->x;
        echo '<br>';
        echo " Value of Y = ".$this->y;
        echo '<br>';
    public function swap1(){
        echo '<h3>The values After Swap Technique 1</h3>';

        $this->a = $this->a+$this->b;
        echo '<br>';
        $this->b = $this->a-$this->b;
        $this->a = $this->a-$this->b;
        echo "value of A= " ."$this->a"."<br>"."value of B= " ." "."$this->b";
    public function swap2(){
    echo '<h3>The values After Swap Technique 2</h3>';
        $this->x = $this->x * $this->y;//$x = 13,$y = 3 now $x =39
        $this->y = $this->x / $this->y;
        $this->x = $this->x / $this->y;
    echo "value of X= " ."$this->x"."<br>"."value of Y= " ." "."$this->y";
Another file to call the methods.I named the file test_swap.php
It contained following codes:
include 'swap_oop.php';
$objSwap = new Swap();

পিএইচপি তে ওওপি দারুন ফান,যারা জাভা হাল্কা পাতলা জানে খুব সহজেই তা পিএইচপিতে ধরতে পারবে।
Check it out .It is fun 😀


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s