This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Functions – 5”.
1. What will be the output of the following PHP code?
<?php
$x = 75;
$y = 25;
function addition()
{
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
addition();
echo $z;
?>
a) 100
b) error
c) 75
d) 25
View Answer
Explanation: 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?
<?php
function 2myfunc()
{
echo "Hello World";
}
2myfunc();
?>
a) Hello World
b) No Output
c) ERROR
d) None of the mentioned
View Answer
Explanation: Function cannot begin with a number.
3. What will be the output of the following PHP code?
<?php
function _func()
{
echo "Hello World";
}
_func();
?>
a) Hello World
b) No Output
c) ERROR
d) None of the mentioned
View Answer
Explanation: Function Beginning with “_” is valid.
4. What will be the output of the following PHP code?
<?php
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
View Answer
Explanation: Function Is case Insensitive.
5. What will be the output of the following PHP code?
<?php
function mine($num)
{
$num = 2 + $num;
echo $num;
}
mine(3);
?>
a) 3
b) $num
c) 5
d) None of the mentioned
View Answer
Explanation: Simple arithmetic operation.
6. What will be the output of the following PHP code?
<?php
function mine($num)
{
$num = 2 + $num;
echo "$num";
}
mine(3);
?>
a) 3
b) $num
c) 5
d) None of the mentioned
View Answer
Explanation: 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?
<?php
function one($string)
{
echo "I am ". $String;
}
one("Batman");
?>
a)
I am Batman
b) I am
c) Batman
d) ERROR
View Answer
Explanation: Variable Undeclared) $string is not the same as $String.
8. What will be the output of the following PHP code?
<?php
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
View Answer
Explanation: 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?
<?php
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
View Answer
Explanation: 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?
<?php
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
View Answer
Explanation: Simple use of arrays.
Sanfoundry Global Education & Learning Series – PHP Programming.
To practice all areas of PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
- Check MCA Books
- Practice Programming MCQs
- Apply for Programming Internship
- Practice MCA MCQs
- Check Information Technology Books