This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Functions – 4”.
1. What will be the output of the following PHP code?
<?php
function A1($x)
{
switch($x)
{
case 1:
//this statement is the same as if($x == 1)
echo 'Case 1 was executed.';
break;
case 2:
//this statement is the same as if($x == 2)
echo 'Case 2 was executed.';
break;
case 3:
//this statement is the same as if($x == 3)
echo 'Case 3 was executed.';
break;
case 4:
//this statement is the same as if($x == 4)
echo 'Case 4 was executed.';
break;
default:
//this statement is the same as if $x does not equal the other conditions
echo 'Default was executed.';
break;
}
}
A1(9);
?>
a) Case 1 was executed
b) Case 2 was executed
c) Default was executed
d) Case 4 was executed
View Answer
Explanation: The switch statement is executed with $x = 9.
2. What will be the output of the following PHP code?
<?php
function uppercase($string)
{
echo ucwords($string);
}
$wow = "uppercase";
$wow("Time to live king size");
?>
a) TIME TO LIVE KING SIZE
b) Time to live king size
c) Uppercase
d) Time To Live King Size
View Answer
Explanation: The ucwords() function converts the first character of each word in a string to uppercase.
3. What will be the output of the following PHP code?
<?php
function TV($string)
{
echo "my favourite TV show is ".$string;
function b()
{
echo " I am here to spoil this code";
}
}
b();
?>
a) I am here to spoil this code
b) Error
c) My favourite TV show isI am here to spoil this code
d) None of the mentioned
View Answer
Explanation: b is undeclared if TV() is not called first.
4. What will be the output of the following PHP code?
<?php
function TV($string)
{
echo "my favourite TV show is ".$string;
function b()
{
echo " I am here to spoil this code";
}
}
function b()
{
echo " I am here to spoil this code";
}
b();
?>
a) I am here to spoil this code
b) Error
c) my favourite TV show isI am here to spoil this code
d) None of the mentioned
View Answer
Explanation: This one works because b is declared independent of TV() also.
5. What will be the output of the following PHP code?
<?php
function TV($string)
{
echo "my favourite TV show is ".$string;
function b()
{
echo " I am here to spoil this code";
}
}
function b()
{
echo " I am here to spoil this code";
}
b();
TV("Sherlock");
?>
a) I am here to spoil this code
b) Error
c) My favourite TV show isI am here to spoil this code
d) None of the mentioned
View Answer
Explanation: Function b is declared twice.
6. What will be the output of the following PHP code?
<?php
function TV($string)
{
echo "my favourite TV show is ".$string;
function b()
{
echo " I am here to spoil this code";
}
}
a("Sherlock");
b();
?>
a) I am here to spoil this code
b) Error
c) my favourite TV show is SherlockI am here to spoil this code
d) None of the mentioned
View Answer
Explanation: b is declared as TV() is executed first.
7. What will be the output of the following PHP code?
<?php
function calc($num1, $num2)
{
$total = $num1 * $num2;
}
$result = calc(42, 0);
echo $result;
?>
a) Error
b) 0
c) 42
d) 84
View Answer
Explanation: Function does not return anything.
8. What will be the output of the following PHP code?
<?php
function calc($num1, $num2)
{
$total = $num1 * $num2;
return $total;
}
$result = calc(42, 0);
echo $result;
?>
a) Error
b) 0
c) 42
d) 84
View Answer
Explanation: Function returns $total.
9. What will be the output of the following PHP code?
<?php
$var = 10;
function one()
{
echo $var;
}
one();
?>
a) Error
b) 10
c) No Output
d) None of the Mentioned
View Answer
Explanation: $var is not global and hence is not available for one().
10. What will be the output of the following PHP code?
<?php
function mine($m)
{
if ($m < 0)
echo "less than 0";
if ($ >= 0)
echo "Not True";
}
mine(0);
?>
a) Less Than 0
b) Not True
c) No Output
d) None of the Mentioned
View Answer
Explanation: Argument is 0.
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.
- Practice MCA MCQs
- Practice Programming MCQs
- Check MCA Books
- Check PHP Books
- Apply for Programming Internship