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 / ajay

I checked the answer posted by Sunil. This answer is
correct,the only mistake he did that the program print the
output together as 1020,2010,not like this
10
20


20
10

Is This Answer Correct ?    2 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 / meenakshisundaram

$a = 10;
$b = 20;
echo $a, $b;
swap();
echo $a, $b;

swap(){
list($a, $b) = array($b, $a);
}

Is This Answer Correct ?    1 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 / sangita jain

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

swap();

?>

Is This Answer Correct ?    0 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

One way is:

function swap($x, $y) {
$x ^= $y ^= $x ^= $y;
return array($x,$y);
}

Is This Answer Correct ?    2 Yes 5 No

Post New Answer

More PHP Interview Questions

Where is the submitted form data stored?

0 Answers  


Explain the difference between unlink() and unset()?

0 Answers  


How to find a length of a string in php?

1 Answers  


What are the security measures we have to take for our site not to hack by others.

3 Answers   Infosys, Torque Infotech,


What are escaping characters? Explain with an example?

0 Answers  


What is data structure in php?

0 Answers  


how to add file in php?

1 Answers   CTS,


Is laravel frontend or backend?

0 Answers  


How can we change the value of a constant?

0 Answers  


What is laracast?

0 Answers  


Explain what is the difference between $var and $$var?

0 Answers  


What is curl php?

0 Answers  


Categories