PHP Multiple Choice Questions on “Functions”.
1. How to define a function in PHP?
a) function {function body}
b) data type functionName(parameters) {function body}
c) functionName(parameters) {function body}
d) function functionName(parameters) {function body}
Answer: d
Clarification: PHP allows us to create our own user-defined functions. Any name ending with an open and closed parenthesis is a function. The keyword function is always used to begin a function.
2. Type Hinting was introduced in which version of PHP?
a) PHP 4
b) PHP 5
c) PHP 5.3
d) PHP 6
Answer: b
Clarification: PHP 5 introduced the feature of type hinting. With the help of type hinting, we can specify the expected data type of an argument in a function declaration. First valid types can be the class names for arguments that receive objects and the other are array for those that receive arrays.
3. Which type of function call is used in line 8 in the following PHP code?
-
-
function calc($price, $tax)
-
{
-
$total = $price + $tax;
-
}
-
$pricetag = 15;
-
$taxtag = 3;
-
calc($pricetag, $taxtag);
-
?>
a) Call By Value
b) Call By Reference
c) Default Argument Value
d) Type Hinting
Answer: a
Clarification: If we call a function by value, we actually pass the values of the arguments which are stored or copied into the formal parameters of the function. Hence, the original values are unchanged only the parameters inside the function changes.
4. What will be the output of the following PHP code?
-
-
function calc($price, $tax="")
-
{
-
$total = $price + ($price * $tax);
-
echo "$total";
-
}
-
calc(42);
-
?>
a) Error
b) 0
c) 42
d) 84
Answer: c
Clarification: You can designate certain arguments as optional by placing them at the end of the list and assigning them a default value of nothing.
5. Which of the following are valid function names?
i) function() ii) €() iii) .function() iv) $function()
a) Only i)
b) Only ii)
c) i) and ii)
d) iii) and iv)
Answer: b
Clarification: A valid function name can start with a letter or underscore, followed by any number of letters, numbers, or underscores. According to the specified regular expression ([a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*), a function name like this one is valid.
6. What will be the output of the following PHP code?
-
-
function a()
-
{
-
function b()
-
{
-
echo 'I am b';
-
}
-
echo 'I am a';
-
}
-
a();
-
a();
-
?>
a) I am a
b) I am bI am a
c) Error
d) I am a Error
Answer: a
Clarification: The output will be “I am a” as we are calling a(); so the statement outside the block of function b() will be called.
7. What will be the output of the following PHP code?
-
-
function a()
-
{
-
function b()
-
{
-
echo 'I am b';
-
}
-
echo 'I am a';
-
}
-
b();
-
a();
-
?>
a) I am b
b) I am bI am a
c) Error
d) I am a Error
Answer: c
Clarification: The output will be Fatal error: Call to undefined function b(). You cannot call a function which is inside a function without calling the outside function first. It should be a(); then b();
8. What will be the output of the following PHP code?
-
-
$op2 = "blabla";
-
function foo($op1)
-
{
-
echo $op1;
-
echo $op2;
-
}
-
foo("hello");
-
?>
a) helloblabla
b) Error
c) hello
d) helloblablablabla
Answer: c
Clarification: If u want to put some variables in function that was not passed by it, you must use “global”. Inside the function type global $op2.
9. A function in PHP which starts with __ (double underscore) is known as __________
a) Magic Function
b) Inbuilt Function
c) Default Function
d) User Defined Function
Answer: a
Clarification: PHP functions that start with a double underscore – a “__” – are called magic functions in PHP. They are functions that are always defined inside classes, and are not stand-alone functions.
10. What will be the output of the following PHP code?
-
-
function foo($msg)
-
{
-
echo "$msg";
-
}
-
$var1 = "foo";
-
$var1("will this work");
-
?>
a) Error
b) $msg
c) 0
d) Will this work
Answer: d
Clarification: It is possible to call a function using a variable which stores the function name.