This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Functions – 2”.
1. What will be the output of the following PHP code?
<?php
function sum($num1, $num2)
{
$total = $num1 + $num2;
echo "chr($total)";
}
$var1 = "sum";
$var1(5, 44);
?>
a) Error
b) 49
c) 1
d) Sum
View Answer
Explanation: It is possible to call a function using a variable which stores the function name also the chr() function returns a character from the specified ASCII value.
2. What will be the output of the following PHP code?
<?php
function sum($num1, $num2)
{
$total = $num1 + $num2;
echo "cos($total)";
}
sum(5,-5);
?>
a) 0
b) 1
c) 0.5
d) -0.5
View Answer
Explanation: cos() gives the cos value of the argument. Here the function returns 1.
3. What will be the output of the following PHP code?
<?php
function b()
{
echo "b is executed";
}
function a()
{
b();
echo "a is executed";
b();
}
a();
?>
a) b is executedb is executedb is executed
b) b is executeda is executed
c) a is executed
d) b is executeda is executedb is executed
View Answer
Explanation: Simple order of execution.
4. What will be the output of the following PHP code?
<?php
function sum($x, $y)
{
$z = $x + $y;
return $z;
}
echo "5 + 10 = " . sum(7,13) . "<br>";
echo "7 + 13 = " . sum(2,4) . "<br>";
echo "2 + 4 = " . sum(5,10);
?>
a)
5 + 10 = 15 2 + 4 = 6 7 + 13 = 20
b)
7 + 13 = 20 5 + 10 = 15 2 + 4 = 6
c)
5 + 10 = 15 7 + 13 = 20 2 + 4 = 6
d)
5 + 10 = 20 7 + 13 = 6 2 + 4 = 15View Answer
Explanation: The function calls are jumbled.
5. What will be the output of the following PHP code?
<?php
function addFive($num)
{
$num += 5;
}
function addSix(&$num)
{
$num += 6;
}
$orignum = 10;
addFive( &$orignum );
echo "Original Value is $orignum<br />";
addSix( $orignum );
echo "Original Value is $orignum<br />";
?>
a)
Original Value is 15 Original Value is 21
b)
Original Value is 15 Original Value is 21
c)
Original Value is 15 Original Value is 15
d) None Of The mentioned
View Answer
Explanation: addSix() passes value of the variable by reference.
6. 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: Functions returns value 30.
7. What will be the output of the following PHP code?
<?php
function sayHello()
{
echo "HelloWorld<br />";
}
$function_holder = "sayHello";
$function_holder();
?>
a) No Output
b) Error
c) sayHello
d) HelloWorld
View Answer
Explanation: It is possible to assign function names as strings to variables and then treat these variables exactly as you would the function name itself.
8. What will be the output of the following PHP code?
<?php
function one()
{
echo " this works";
function two()
{
echo "this too works";
}
}
one();
two();
?>
a) error
b) this works
c) this worksthis too works
d) this works this too works
View Answer
Explanation: Two is declared in one and is called after one. Hence it works.
9. What will be the output of the following PHP code?
<?php
function do($myString)
{
echo strpos($myString, "donkey",0);
}
do("The donkey looks like a horse.");
?>
a) 4
b) 5
c) 2
d) None of the mentioned
View Answer
Explanation: Donkey starts from position 4 in string.
10. What will be the output of the following PHP code?
<?php
function one()
{
define("const","I am awesome!");
echo constant("const");
}
one();
?>
a) I am awesome!!
b) const
c) const, I am awesome!!
d) “const”,”I am awesome!”
View Answer
Explanation: Using the define function to define the constant “const”.
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.
- Check PHP Books
- Check MCA Books
- Check Information Technology Books
- Practice MCA MCQs
- Practice Programming MCQs