Functions — PHP Technical Interview Questions & Answers
Functions
Functions
- Functions is a PHP topic. In plain words you use it for function add($a, $b) in a marks helper. Don’t start with a slogan — start with that picture.
- Smallest example: function name($x) { return $x; }. Type it, run it, and say what you see. If you can do that from memory, you know Functions.
- From the example next to this theory: Functions — typed params and return types make PHP APIs clearer.
- Trap: no return so the caller gets null. Fix that before you talk about advanced PHP.
- Viva: what is Functions? Then show function name($x) { return $x; }. Then name the trap.
Technical Interview Tip: What is Functions? Show this: function name($x) { return $x; }. Trap: no return so the caller gets null.
Code Examples for Functions
Functions — working example
<?php
function greet(string $name, int $n = 1): string {
return str_repeat("Hi $name! ", $n);
}
echo greet("Asha");
echo greet("Dev", 2);Functions — typed params and return types make PHP APIs clearer.
How to Explain Functions to an Interviewer
What is Functions?
Used for function add($a, $b) a marks helper Not just the heading
Give one small example of Functions.
function name($x) { return $x; } Functions — typed params and return types make PHP APIs clearer
What mistake do beginners make with Functions?
no return so the caller gets null Show the fix
Where do you use Functions?
a marks helper function add($a, $b)