PHP functions are a core aspect of the language, enabling code reusability and modularity. In this guide, we dive deep into defining functions, handling parameters, understanding variable scopes, recursion, and leveraging built-in PHP functions to optimize your web development skills. Here are 30 multiple-choice questions (MCQs) covering each of these topics.
PHP Functions – MCQs
1. Defining Functions
Which keyword is used to define a function in PHP?
a) define
b) function
c) func
d) method
What is the correct way to call a user-defined function in PHP?
a) call functionName();
b) function functionName();
c) functionName();
d) exec functionName();
Which of the following is a valid PHP function name?
a) 2function
b) my-function
c) _function
d) function name
Can a function in PHP return multiple values directly?
a) Yes
b) No
c) Only with arrays
d) Only with objects
What is the default return value of a PHP function if no return statement is provided?
a) 0
b) null
c) false
d) undefined
How do you pass arguments by reference in PHP?
a) Using &
b) Using *
c) Using @
d) Using %
2. Function Parameters and Return Values
What will happen if a function is called with fewer arguments than it is defined to accept?
a) Error
b) Warning
c) Ignored
d) None of the above
What is the correct syntax for defining default parameter values in PHP?
a) function myFunction($param=2)
b) function myFunction($param: 2)
c) function myFunction($param -> 2)
d) function myFunction($param = 2)
Can you specify the data type of a function parameter in PHP?
a) Yes, using type hinting
b) No, PHP does not support it
c) Only in PHP 8+
d) Only for objects and arrays
Which of the following is a valid return type declaration in PHP 7+?
a) function myFunction(): int
b) function myFunction() -> int
c) function myFunction() => int
d) function myFunction() == int
What is the output of the following code? function test($a, $b = 2) { return $a * $b; } echo test(4);
a) 4
b) 6
c) 8
d) Error
How can you return multiple values from a PHP function?
a) Using arrays
b) Using objects
c) Using references
d) All of the above
3. Variable Scope (Global vs Local)
What is the scope of a variable defined inside a function?
a) Global
b) Local
c) Static
d) Public
How can you access a global variable inside a function?
a) By using global keyword
b) By using $GLOBALS array
c) By passing it as a parameter
d) All of the above
What does the static keyword do in a function?
a) Makes variable global
b) Preserves the value of a variable between function calls
c) Makes variable accessible in all files
d) Deletes the variable after use
What will the following code output? $x = 10; function demo() { global $x; $x = $x + 5; } demo(); echo $x;
a) 10
b) 15
c) 5
d) Error
Can a function have both global and local variables with the same name?
a) Yes, but only one is accessible
b) Yes, and both are accessible
c) No, PHP will throw an error
d) Only if defined with static
What is the use of $GLOBALS array?
a) It stores all local variables
b) It stores all global variables
c) It stores all static variables
d) It stores all session variables
4. Recursion in Functions
What is recursion in PHP?
a) A function calling itself
b) A function calling another function
c) Infinite loop in a function
d) Calling a function multiple times
Which of the following is a base case in recursion?
a) When a function calls itself
b) When the recursion stops
c) When an error occurs
d) None of the above
What will the following recursive function return? function factorial($n) { if ($n <= 1) return 1; else return $n * factorial($n - 1); } echo factorial(4);
a) 10
b) 12
c) 24
d) 16
What can happen if a recursive function does not have a base case?
a) Infinite loop
b) Compilation error
c) Memory overflow
d) None of the above
What is the maximum recursion depth in PHP by default?
a) 100
b) 256
c) 512
d) 1000
How can you prevent infinite recursion in PHP?
a) By defining a base case
b) By using global variables
c) By using a static keyword
d) By limiting function calls
5. Built-in PHP Functions
Which function is used to get the length of a string in PHP?
a) strlen()
b) length()
c) strlength()
d) sizeOf()
What is the purpose of isset() function?
a) Check if a variable is set and not null
b) Check if a variable is false
c) Check if a variable is empty
d) Check if a variable is undefined
What does the explode() function do?
a) Joins array elements into a string
b) Splits a string into an array
c) Removes elements from an array
d) Reverses a string
How do you round a number to the nearest integer in PHP?
a) round()
b) ceil()
c) floor()
d) int()
What is the output of the following code?phpCopy codeecho strtoupper("hello world");
a) hello world
b) Hello World
c) HELLO WORLD
d) Error
Which function is used to include a file in PHP?
a) add()
b) include()
c) require_once()
d) Both b and c
Answers
QNo
Answer
1
b) function
2
c) functionName()
3
c) _function
4
c) Only with arrays
5
b) null
6
a) Using &
7
a) Error
8
d) function myFunction($param = 2)
9
a) Yes, using type hinting
10
a) function myFunction(): int
11
c) 8
12
d) All of the above
13
b) Local
14
d) All of the above
15
b) Preserves the value of a variable between function calls