Q1. What are parameters and arguments?
A parameter is the variable in the function definition. An argument is the value you pass when calling it.
Q2. How do default and named arguments work?
A default makes a parameter optional. Named arguments (PHP 8.0) pass values by parameter name, so you can skip optional ones.
<?php
function box(int $width, int $height = 1, bool $filled = false): string
{
return "$width x $height" . ($filled ? " filled" : "");
}
echo box(3, filled: true);
3 x 1 filled
Q3. What is a variadic function?
A function that accepts any number of arguments with ...$args. The arguments arrive as an array. The same ... in a call unpacks an array into arguments.
<?php
function sum(int ...$n): int
{
return array_sum($n);
}
echo sum(1, 2, 3) . " " . sum(...[4, 5]);
6 9
Q4. What are return types? Name some.
A type after the parameter list: : int, : string, : ?array (nullable), : int|string (union), : void (returns nothing), : never (never returns), : static.
Q5. What is the difference between an anonymous function and an arrow function?
An anonymous function (function () use ($x) { ... }) can have many statements and needs use to see outside variables. An arrow function (fn($a) => expr, PHP 7.4) has a single expression and captures outside variables by value automatically.
Q6. What is a closure?
A function that remembers variables from the scope where it was created. In PHP every anonymous function is a Closure object. use (&$x) captures by reference.
<?php
function counter(): Closure
{
$count = 0;
return function () use (&$count): int {
return ++$count;
};
}
$c = counter();
$c();
$c();
echo $c();
3
Q7. What is a callback and what formats can it take?
A function passed to another function to be called later. Formats: a function name string ("strtoupper"), a closure, an array [$object, "method"], a static method string "Class::method", and first-class callable syntax (strlen(...), PHP 8.1).
Q8. What is recursion? What do you need to avoid problems?
A function that calls itself. It needs a base case that stops it. Deep recursion uses memory, and PHP does not optimise tail calls, so loops are better for simple repetition.
<?php
function factorial(int $n): int
{
return $n <= 1 ? 1 : $n * factorial($n - 1);
}
echo factorial(6);
720
Q9. What does the static keyword do inside a function?
A static variable keeps its value between calls of that function.
<?php
function visits(): int
{
static $n = 0;
return ++$n;
}
visits();
echo visits();
2
Q10. Why can a function not see a global variable?
PHP functions have their own scope. Pass what you need as parameters (best), or use global/$GLOBALS, which hides dependencies and is best avoided.
Q11. What happens if you pass the wrong type to a typed parameter?
Without strict types, PHP converts if it can ("5" to 5) and otherwise throws a TypeError. With strict_types=1, it throws unless the type matches exactly.
Q12. Predict the output.
<?php
function add(int $a, int $b = 10)
{
return $a + $b;
}
echo add(b: 5, a: 1) . " " . add(2);
6 12
Q13. What is the difference between array_map, array_filter and array_reduce?
array_map transforms every item and returns a new array. array_filter keeps items for which the callback returns true (and preserves keys). array_reduce combines all items into one value.
Q14. What is a pure function?
A function whose result depends only on its arguments and which has no side effects (no output, no global state changes). Pure functions are easy to test and reason about.
Q15. How would you make a function accept either an int or a string?
With a union type: function id(int|string $id): string. Convert to one type early inside the function.