MCQs on Functions | PHP Basics

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

  1. Which keyword is used to define a function in PHP?
    • a) define
    • b) function
    • c) func
    • d) method
  2. What is the correct way to call a user-defined function in PHP?
    • a) call functionName();
    • b) function functionName();
    • c) functionName();
    • d) exec functionName();
  3. Which of the following is a valid PHP function name?
    • a) 2function
    • b) my-function
    • c) _function
    • d) function name
  4. Can a function in PHP return multiple values directly?
    • a) Yes
    • b) No
    • c) Only with arrays
    • d) Only with objects
  5. What is the default return value of a PHP function if no return statement is provided?
    • a) 0
    • b) null
    • c) false
    • d) undefined
  6. How do you pass arguments by reference in PHP?
    • a) Using &
    • b) Using *
    • c) Using @
    • d) Using %

2. Function Parameters and Return Values

  1. 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
  2. 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)
  3. 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
  4. 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
  5. 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
  6. 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)

  1. What is the scope of a variable defined inside a function?
    • a) Global
    • b) Local
    • c) Static
    • d) Public
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. What is the maximum recursion depth in PHP by default?
    • a) 100
    • b) 256
    • c) 512
    • d) 1000
  6. 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

  1. Which function is used to get the length of a string in PHP?
    • a) strlen()
    • b) length()
    • c) strlength()
    • d) sizeOf()
  2. 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
  3. 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
  4. How do you round a number to the nearest integer in PHP?
    • a) round()
    • b) ceil()
    • c) floor()
    • d) int()
  5. What is the output of the following code?phpCopy codeecho strtoupper("hello world");
    • a) hello world
    • b) Hello World
    • c) HELLO WORLD
    • d) Error
  6. Which function is used to include a file in PHP?
    • a) add()
    • b) include()
    • c) require_once()
    • d) Both b and c

Answers

QNoAnswer
1b) function
2c) functionName()
3c) _function
4c) Only with arrays
5b) null
6a) Using &
7a) Error
8d) function myFunction($param = 2)
9a) Yes, using type hinting
10a) function myFunction(): int
11c) 8
12d) All of the above
13b) Local
14d) All of the above
15b) Preserves the value of a variable between function calls
16b) 15
17a) Yes, but only one is accessible
18b) It stores all global variables
19a) A function calling itself
20b) When the recursion stops
21c) 24
22c) Memory overflow
23d) 1000
24a) By defining a base case
25a) strlen()
26a) Check if a variable is set and not null
27b) Splits a string into an array
28a) round()

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top