This set of PHP Multiple Choice Questions & Answers (MCQs) focuses 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}
View Answer
Explanation: 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
View Answer
Explanation: 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?
<?php
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
View Answer
Explanation: 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?
<?php
function calc($price, $tax="")
{
$total = $price + ($price * $tax);
echo "$total";
}
calc(42);
?>
a) Error
b) 0
c) 42
d) 84
View Answer
Explanation: 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)
View Answer
Explanation: 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?
<?php
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
View Answer
Explanation: Since we are calling the function a() 2 times, the output will be “I am a” followed by a Fatal Error as we are redeclaring function b() during second call of function a().
7. What will be the output of the following PHP code?
<?php
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
View Answer
Explanation: 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?
<?php
$op2 = "blabla";
function foo($op1)
{
echo $op1;
echo $op2;
}
foo("hello");
?>
a) helloblabla
b) Error
c) hello
d) helloblablablabla
View Answer
Explanation: 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
View Answer
Explanation: 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?
<?php
function foo($msg)
{
echo "$msg";
}
$var1 = "foo";
$var1("will this work");
?>
a) Error
b) $msg
c) 0
d) Will this work
View Answer
Explanation: It is possible to call a function using a variable which stores the function name.
More MCQs on PHP Functions:
- PHP Functions MCQ (Set 2)
- PHP Functions MCQ (Set 3)
- PHP Functions MCQ (Set 4)
- PHP Functions MCQ (Set 5)
- PHP Functions MCQ (Set 6)
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 Information Technology Books
- Practice Programming MCQs
- Apply for Programming Internship
- Check MCA Books
- Practice MCA MCQs