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 ?    79 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 ?    44 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

how to store date to database

5 Answers   ConSim,


Explain what does the expression exception::__tostring means?

0 Answers  


What is a simple php method to make a cross domain data request?

0 Answers  


How do you achieve page caching in PHP?

3 Answers  


Which is the best method to fetch the data from mysql? 1.mysql_fetch_array() 2.mysql_fetch_object() 3.mysql_fetch_row() 4.mysql_fetch_assoc()

6 Answers  






What are php string functions?

0 Answers  


write a note on Testing the web site

0 Answers  


What is .htaccessfile and use of this file

5 Answers  


what is the best function that can be used to connect to mysql database and in what cases that we can use below functions ? 1. mysql_connect() 2. mysql_pconnect() please give your answer with all pros and cons

1 Answers  


How can you create a session in php?

0 Answers  


Is facebook still in php?

0 Answers  


What is the use of inner join in mysql?

0 Answers  


Categories