Mastering PHP’s advanced functions, including anonymous functions, recursion, callbacks, and namespaces, is crucial for writing clean, efficient, and maintainable code. Explore these advanced concepts to enhance your PHP skills.
Multiple Choice Questions (MCQs)
1. Anonymous Functions and Closures
What is an anonymous function in PHP? a) A function without a name that is declared inline b) A function that has no parameters c) A function that returns nothing d) A function with no body
How do you assign an anonymous function to a variable in PHP? a) $func = function() { ... }; b) function() { ... } = $func; c) anon_func() => $func; d) func => $func;
Which of the following is correct about closures in PHP? a) A closure has access to the variables of its surrounding scope b) Closures cannot access variables outside their own scope c) Closures are always anonymous functions d) Closures are only available in object-oriented programming
How do you pass variables by reference into an anonymous function in PHP? a) function(&$variable) { ... } b) function($variable) { ... } c) function($variable = ref) { ... } d) function($&variable) { ... }
Which of the following will output 10? $a = 10; $closure = function() use ($a) { echo $a; }; $closure(); a) 10 b) 0 c) Error d) NULL
2. Variable Functions
What is the purpose of variable functions in PHP? a) To call a function dynamically using a variable containing the function name b) To declare a function without parameters c) To store the result of a function in a variable d) To pass variables to functions
Which of the following is the correct way to invoke a variable function in PHP? a) $func_name($arg); b) $arg->$func_name(); c) func->$arg(); d) $arg.func_name();
How do you assign a function to a variable in PHP? a) $func = 'function_name'; b) $func = function_name; c) $func = new function_name; d) $func = function() { ... };
What is the output of the following code? $func = 'strlen'; echo $func("Hello"); a) 5 b) Hello c) Error d) NULL
Which function allows you to invoke a method dynamically from a string variable? a) call_user_func() b) dynamic_method() c) call_method() d) invoke_func()
3. Recursion and Iteration
What is recursion in PHP? a) A function that calls itself b) A function that calls another function c) A loop that repeats itself d) A method that calls a class
What will happen if a recursive function does not have a base case? a) The function will execute indefinitely and eventually cause a stack overflow b) The function will stop after a certain number of iterations c) An error message will appear d) The function will be skipped
Which of the following is a correct recursive function in PHP? function factorial($n) { if ($n == 0) return 1; return $n * factorial($n - 1); } a) A function that calculates factorial b) A function with a loop c) A function that calls another function d) A function that uses a callback
Which of the following is true about iteration in PHP? a) Iteration is an alternative to recursion when dealing with loops b) Iteration always causes a stack overflow c) Iteration is used for objects only d) Iteration only works with arrays
What is the key advantage of using iteration over recursion? a) It uses less memory b) It is easier to debug c) It always produces more accurate results d) It is more efficient for complex algorithms
4. Callback Functions and Function Hooks
What is a callback function in PHP? a) A function passed as an argument to another function b) A function that is executed automatically by PHP c) A function that calls itself d) A function that returns another function
How do you pass a callback function to array_map()? a) array_map(callback, $arr); b) array_map($arr, callback); c) array_map(callback(), $arr); d) array_map($arr);
What is the purpose of call_user_func() in PHP? a) It calls a function dynamically, passing parameters b) It declares a function c) It filters array elements d) It defines a class
How do you register a function as a callback in PHP? a) function_register(callback); b) callback_register('callback'); c) register_function(callback); d) register_shutdown_function('callback');
Which of the following is an example of a function hook in WordPress? a) add_action('hook_name', 'callback_function'); b) hook_function('callback'); c) register_hook('callback'); d) function_hook('callback');
5. Namespaces and Autoloading
What is the purpose of namespaces in PHP? a) To encapsulate classes, functions, and constants to avoid name conflicts b) To execute PHP functions dynamically c) To optimize code execution d) To load classes automatically
Which keyword is used to declare a namespace in PHP? a) namespace b) use c) namespace_of d) namespace_start
What is autoloading in PHP? a) The automatic inclusion of class files when they are needed b) A function that loads libraries c) A mechanism to execute functions automatically d) A method to load database queries
Which function registers an autoloader in PHP? a) spl_autoload_register() b) autoload_function() c) register_autoload() d) auto_load_classes()
Which of the following best describes the use keyword in PHP? a) It imports a class or namespace into the current file b) It defines a constant c) It creates an instance of a class d) It binds a function to a class
Which is the correct way to include an external PHP class using autoloading? a) require_once('MyClass.php'); b) autoload('MyClass'); c) include_once('MyClass.php'); d) spl_autoload_register('autoload');
How do you access a class inside a namespace in PHP? a) namespace\ClassName b) use ClassName; c) ClassName::namespace d) ClassName@namespace
What happens if an undefined class is called without autoloading enabled? a) A fatal error occurs b) PHP throws a warning c) PHP will automatically try to include the class file d) Nothing happens
Which statement allows importing a class from a namespace into the current scope? a) use namespace\ClassName; b) import namespace\ClassName; c) require namespace\ClassName; d) include namespace\ClassName;
What will happen if you call a class from a non-existent namespace? a) PHP will throw an error b) The function will return null c) PHP will attempt to load it from the default namespace d) PHP will ignore the call
Answer Key
Qno
Answer
1
a) A function without a name that is declared inline
2
a) $func = function() { ... };
3
a) A closure has access to the variables of its surrounding scope
4
a) function(&$variable) { ... }
5
a) 10
6
a) To call a function dynamically using a variable containing the function name
7
a) $func_name($arg);
8
a) $func = 'function_name';
9
a) 5
10
a) call_user_func()
11
a) A function that calls itself
12
a) The function will execute indefinitely and eventually cause a stack overflow
13
a) A function that calculates factorial
14
a) Iteration is an alternative to recursion when dealing with loops
15
a) It uses less memory
16
a) A function passed as an argument to another function
17
a) array_map(callback, $arr);
18
a) It calls a function dynamically, passing parameters
19
d) register_shutdown_function('callback');
20
a) add_action('hook_name', 'callback_function');
21
a) To encapsulate classes, functions, and constants to avoid name conflicts
22
a) namespace
23
a) The automatic inclusion of class files when they are needed
24
a) spl_autoload_register()
25
a) It imports a class or namespace into the current file