This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Functions – 6”.
1. What will be the output of the following PHP code?
<?php
function colour()
{
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value)
{
echo "$value <br>";
}
}
colour();
?>
a)
red green blue yellow
b)
green blue yellow red
c)
red blue yellow green
d)
red green yellow blueView Answer
Explanation: For each function causes the program to loop through each array value once.
2. What will be the output of the following PHP code?
<?php
function addFunction($num1, $num2)
{
$sum = $num1 + $num2;
return $sum;
}
$return_value = addFunction(10, 20);
echo "Returned value from the function : " .$return_value
?>
a) Returned value from the function : $return_value
b) Error
c) Returned value from the function : 30
d) Returned value from the function :
View Answer
Explanation: Function returns 30.
3. What will be the output of the following PHP code?
<?php
function time($string)
{
echo strtr("Towe Pa55", "ow5", $string);
}
time("ims");
?>
a) Time Pa55
b) Towe Pa55
c) Towe Pass
d) Time Pass
View Answer
Explanation: The strtr() function translates certain characters in a string.
4. What will be the output of the following PHP code?
<?php
function constant()
{
define("GREETING", "Welcome to Narnia");
echo greeting;
}
?>
a) Welcome to Narnia
b) greeting
c) GREETING
d) ERROR
View Answer
Explanation: By default constants are case sensitive. Hence an error will arise.
5. What will be the output of the following PHP code?
<?php
function constant()
{
define("GREETING", "Welcome to Narnia",true);
echo greeting;
}
?>
a) Welcome to Narnia
b) greeting
c) GREETING
d) ERROR
View Answer
Explanation: By default constants are case sensitive. But the third parameter in define(), if set to true, makes constants case insensitive.
6. What will be the output of the following PHP code?
<?php
function start($string)
{
if ($string < 45)
return 20;
else
return 40;
}
$t = start(90);
if ($t < 20)
{
echo "Have a good day!";
}
else
{
echo "Have a good night!";
}
?>
a) Have a good day!
b) Have a good night!
c) ERROR
d) None of the mentioned
View Answer
Explanation: Function returns 40. This is greater than 20, hence the output.
7. What will be the output of the following PHP code?
<?php
function case()
{
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
}
case();
?>
a) Hello World!
b)
Hello World! Hello World!
c)
Hello World! Hello World! Hello World!
d) None of the mentioned
View Answer
Explanation: Functions, keywords etc in php are case insensitive.
8. What will be the output of the following PHP code?
<?php
function email()
{
$email = ’user@yahoo.com’;
$new = strstr($email, ‘@');
echo $new;
}
email();
?>
a) user
b) [email protected]
c) @yahoo.com
d) yahoo.com
View Answer
Explanation: The strstr() function searches for the first occurrence of a string inside another string.
9. What will be the output of the following PHP code?
<?php
function string()
{
echo strstr("Hello world!", 111);
}
string();
?>
a) o world!
b) Hello world!
c) 111
d) No Output
View Answer
Explanation: 111 is the ASCII value of o.
10. What will be the output of the following PHP code?
<?php
function CalAll($x,$y)
{
echo ($x + $y);
echo "<br>";
echo ($x - $y);
echo "<br>";
echo ($x * $y);
echo "<br>";
echo ($x / $y);
echo "<br>";
echo ($x % $y);
}
$x = 10;
$y = 6;
CalcAll();
?>
a)
4 60 1.6666666666667 4 16
b)
16 4 60 1.6666666666667 4
c)
4 16 4 60 1.6666666666667
d)
1.6666666666667 4 16 4 60View Answer
Explanation: Simple usage of all arithmetic operators.
Sanfoundry Global Education & Learning Series – PHP Programming.
To practice all questions on PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
- Apply for Programming Internship
- Practice MCA MCQs
- Check PHP Books
- Check MCA Books
- Check Information Technology Books