Write a Program to swap two numbers in PHP

<?php 

   $a = 10;
   $b = 20;

   echo 'Before Swaping A = '.$a.' and B = '.$b;

   $temp = $a + $b;

   $a = $temp - $a;

   $b = $temp - $b;

   echo 'After Swaping A = '.$a.' and B = '.$b;
   

   // swaping without using any third variable 

   $a = 10;
   $b = 20;

   echo 'Before Swaping A = '.$a.' and B = '.$b;

   $a = ( $a + $b ) - ( $b = $a );

   echo 'After Swaping A = '.$a.' and B = '.$b;


  ?>

Leave a Comment