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
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
What is difference between php and wordpress?
Require_once(), require(), include(). What is difference between them?
What is the difference between for and foreach loop in php?
Is php free to use?
What is session in php why it is use?
What is a role of a designer in web development. Till what level the designer will do his work. Does he gives the complete html file or the tpl file or he should also include the smarty tag in the tpl file.
What is a php array?
Explain Type juggling?
What is alias in php?
What are the rules in creating php variable?
Write down the code for save an uploaded file in php.
Tell me what are sql injections, how do you prevent them and what are the best practices?