1. Write a function to swap two values ?
$a = 10;
$b = 20;
echo $a, $b;
swap(); // If u want u can pass parameters
echo $a, $b; // It should print 20 , 10

Answers were Sorted based on User's Feedback



1. Write a function to swap two values ? $a = 10; $b = 20; echo $a, $b; swap(); // If u want u c..

Answer / binoyav

$a = 10;
$b = 20;

echo $a, $b;
echo "<br>";
swap($a, $b);
echo $a, $b;
function swap(&$a, &$b)
{
$temp = $a;
$a = $b;
$b = $temp;
}

Is This Answer Correct ?    80 Yes 6 No

1. Write a function to swap two values ? $a = 10; $b = 20; echo $a, $b; swap(); // If u want u c..

Answer / vivek srivastava

<?php
$a=10;
$b=20;
echo $a,$b;
echo "<br>";
swap($a, $b);
echo "<br>";
echo $a,$b;
function swap(&$a, &$b)
{
$a = $a + $b;
$b = $a - $b;
$a = $a - $b;
}
?>

Is This Answer Correct ?    31 Yes 8 No

1. Write a function to swap two values ? $a = 10; $b = 20; echo $a, $b; swap(); // If u want u c..

Answer / ranjan

function swap(){

$a = 10;
$b = 20;

$a = $a + $b;
$b = $a - $b;
$a = $a - $b;
echo $a, $b;

}

Is This Answer Correct ?    45 Yes 25 No

1. Write a function to swap two values ? $a = 10; $b = 20; echo $a, $b; swap(); // If u want u c..

Answer / ankush sharma

<?php
$a=10;
$b=20;
echo $a,$b;
echo "<br>";
swap($a, $b);
echo "<br>";
echo $a,$b;
function swap(&$a, &$b)
{
$a = $a + $b;
$b = $a - $b;
$a = $a - $b;
}
?>

Is This Answer Correct ?    14 Yes 2 No

1. Write a function to swap two values ? $a = 10; $b = 20; echo $a, $b; swap(); // If u want u c..

Answer / ramse

yes we can do this also.
But pass by reference allow in PHP5 not in PHP4.

Is This Answer Correct ?    8 Yes 0 No

1. Write a function to swap two values ? $a = 10; $b = 20; echo $a, $b; swap(); // If u want u c..

Answer / nikunj kansara

$x=$a;
$a=$b;
$b=$x;
echo $a.",".$b;

Is This Answer Correct ?    11 Yes 4 No

1. Write a function to swap two values ? $a = 10; $b = 20; echo $a, $b; swap(); // If u want u c..

Answer / sunil kumar

<?php
$a = 10;
$b = 20;
echo $a, $b;
$a=$a+$b;
$b=$a-$b;
$a=$a-$b;
echo "<br>";
echo $a, $b;

?>

Is This Answer Correct ?    9 Yes 2 No

1. Write a function to swap two values ? $a = 10; $b = 20; echo $a, $b; swap(); // If u want u c..

Answer / naresh

$a=10;
$b=10;
list($a,$b) = array($b, $a);
echo "a=" . $a;
echo "b=" . $b;

Is This Answer Correct ?    7 Yes 1 No

1. Write a function to swap two values ? $a = 10; $b = 20; echo $a, $b; swap(); // If u want u c..

Answer / dinesh kumar

function swap($x,$y)
{
$x^=$y^=$x^=$y;
return array($x,$y);
}
list($a,$b)=swap(5,10);
echo $a;
echo $b;

Is This Answer Correct ?    4 Yes 1 No

1. Write a function to swap two values ? $a = 10; $b = 20; echo $a, $b; swap(); // If u want u c..

Answer / binoyav

The above answer is correct. But the interviewer does not
want to echo the values from inside the function. It should
be in the following way
$a = 10;
$b = 20;
echo $a, $b;
swap(); // Here if you want you can pass the variables
echo $a, $b;

function swap()
{
}

Is This Answer Correct ?    8 Yes 7 No

Post New Answer

More PHP Interview Questions

Explain whether it is possible to share a single instance of a memcache between multiple php projects?

0 Answers  


How can we destroy the cookie?

4 Answers  


What is isset php?

0 Answers  


What is difference between session and cookies in php?

0 Answers  


what is PDO?

0 Answers  


Is php a programming language?

0 Answers  


How can we destroy the session, how can we unset the variable of a session?

5 Answers  


How to pass an argument to a function?

0 Answers  


I created a new joomla module for administrator. when am going to install this, it is going "joomla/modules" path. but, i want to install this in the "joomla/administration/modules" path.

2 Answers  


How can we submit from without a submit button?

0 Answers  


What are advantages of .htaccess?

0 Answers  


How many ways can we get the value of current session id?

3 Answers   Infosys, L&T, SysBiz, Torque Infotech,


Categories