PHP Assessment Questions and Answers on “Functions – 5”.
1. What will be the output of the following PHP code?
-
-
$x = 75;
-
$y = 25;
-
function addition()
-
{ -
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
-
} -
addition();
-
echo $z;
-
?>
a) 100
b) error
c) 75
d) 25
Answer: a
Clarification: z is a variable present within the $GLOBALS array, it is also accessible from outside the function!
2. What will be the output of the following PHP code?
-
-
function 2myfunc()
-
{ -
echo "Hello World";
-
} -
2myfunc();
-
?>
a) Hello World
b) No Output
c) ERROR
d) None of the mentioned
Answer: c
Clarification: Function cannot begin with a number.
3. What will be the output of the following PHP code?
-
-
function _func()
-
{ -
echo "Hello World";
-
} -
_func();
-
?>
a) Hello World
b) No Output
c) ERROR
d) None of the mentioned
Answer: a
Clarification: Function Beginning with “_” is valid.
4. What will be the output of the following PHP code?
-
-
function test($int)
-
{ -
if ($int == 1)
-
echo "This Works";
-
if ($int == 2)
-
echo "This Too Seems To Work";
-
} -
test(1);
-
TEST(2);
-
?>
a) This Works
b) This Too Seems To Work
c) This WorksThis Too Seems To Work
d) ERROR
Answer: c
Clarification: Function Is case Insensitive.
5. What will be the output of the following PHP code?
-
-
function mine($num)
-
{ -
$num = 2 + $num;
-
echo $num;
-
} -
mine(3);
-
?>
a) 3
b) $num
c) 5
d) None of the mentioned
Answer: c
Clarification: Simple arithmetic operation.
6. What will be the output of the following PHP code?
-
-
function mine($num)
-
{ -
$num = 2 + $num;
-
echo "$num";
-
} -
mine(3);
-
?>
a) 3
b) $num
c) 5
d) None of the mentioned
Answer: b
Clarification: The function is defined as echo “$num”. This means $num is treated as a string and not as a variable.
7. What will be the output of the following PHP code?
-
-
function one($string)
-
{ -
echo "I am ". $String;
-
} -
one("Batman");
-
?>
a)
I am Batman
b) I am
c) Batman
d) ERROR
Answer: d
Clarification: Variable Undeclared) $string is not the same as $String.
8. What will be the output of the following PHP code?
-
-
function string($title);
-
{ -
$title = ucwords($title);
-
echo lcfirst($title);
-
} -
string("you went full retard");
-
?>
a) You went full retard
b) You Went Full Retard
c) YOU WENT FULL RETARD
d) you Went Full Retard
Answer: d
Clarification: ucwords() changes all the first letters to capitals. lcfirst() changes first letter of a string to small.
9. What will be the output of the following PHP code?
-
-
function multi($num)
-
{ -
if ($num == 3)
-
echo "I Wonder";
-
if ($num == 7)
-
echo "Which One";
-
if ($num == 8)
-
echo "Is The";
-
if ($num == 19)
-
echo "Correct Answer";
-
} -
$can = stripos("I love php, I love php too!","PHP");
-
multi($can);
-
?>
a) I Wonder
b) Which One
c) Is The
d) Correct Answer
Answer: b
Clarification: The stripos() function finds the position of the first occurrence of a string inside another string. In this case it returns 7.
10. What will be the output of the following PHP code?
-
-
function movie($int)
-
{ -
$movies = array("Fight Club", "Kill Bill", "Pulp Fiction");
-
echo "You Do Not Talk About ". $movie[$integer];
-
} -
movie(0);
-
?>
a) You Do Not Talk About Fight Club
b) You Do Not Talk About Kill Bill
c) You Do Not Talk About Pulp Fiction
d) None of the mentioned
