$a=10;$b=20; how to swap with createin gnew variable in php

 To swap the values of two variables $a and $b and create a new variable, you can use a temporary variable to hold one of the values temporarily. Here's an example:


 s

$a = 10;
$b = 20;

$tmp = $a; // Store the value of $a in a temporary variable
$a = $b; // Assign the value of $b to $a
$b = $tmp; // Assign the value of the temporary variable to $b

// Now $a and $b are swapped, and the original values are stored in $tmp

echo "a: " . $a . "<br>"; // Outputs: 20
echo "b: " . $b . "<br>"; // Outputs: 10

In the example above, we use a temporary variable $tmp to store the value of $a temporarily. Then we assign the value of $b to $a and finally assign the value of $tmp (which holds the original value of $a) to $b.

After swapping the values, you can use the new variables $a and $b to perform further operations or output the results as needed.

Note that by swapping the values of $a and $b with a temporary variable, the original values of $a and $b are preserved in $tmp.


In the given program, $tmp is indeed a variable. It is used as a temporary variable to store the value of $a so that it can be swapped with the value of $b.

In the program


$tmp = $a; // Store the value of $a in a temporary variable $a = $b; // Assign the value of $b to $a $b = $tmp; // Assign the value of the temporary variable to $b



The variable $tmp is assigned the value of $a using the assignment operator =, which indicates that it is a variable. Then the value of $b is assigned to $a, effectively swapping their values. Finally, the value of the temporary variable $tmp is assigned back to $b, completing the value swap.

Variables in PHP are represented by a dollar sign ($) followed by the variable name. In this case, $tmp is a valid variable and can be used to store data temporarily during the swapping process.

No comments:

Post a Comment

Event listening in react

 How we can listen to som eevents some envents fire like click or automatically user enters into input button , that is event on word type i...