PHP Functions – Interview Questions


🟢 Basic Level

1️⃣ What is a function in PHP?

👉 A function is a reusable block of code that performs a specific task.


2️⃣ How do you define a function in PHP?

function myFunction() {
echo "Hello";
}

3️⃣ What is the difference between echo and return?

👉 echo → outputs directly
👉 return → sends value back to caller


4️⃣ What are function parameters?

👉 Variables passed into a function as input.

function add($a, $b) {
return $a + $b;
}

5️⃣ What are default arguments?

👉 Predefined values used if no argument is passed.

function greet($name = "Guest") {
echo $name;
}

🟡 Intermediate Level


6️⃣ What is the difference between pass by value and pass by reference?

👉 Pass by value (default):

function test($x) {
$x = 10;
}

👉 Pass by reference:

function test(&$x) {
$x = 10;
}

📌 & allows modifying original variable


7️⃣ What are variable functions in PHP?

👉 Calling functions using a variable name.

function hello() {
echo "Hi";
}$f = "hello";
$f();

8️⃣ What is a recursive function?

👉 A function that calls itself.

function countDown($n) {
if ($n <= 0) return;
echo $n;
countDown($n - 1);
}

9️⃣ What are built-in functions?

👉 Predefined PHP functions like:

  • strlen()
  • count()
  • array_merge()

🔟 What is function overloading in PHP?

👉 PHP does NOT support traditional function overloading.

But you can simulate it using:

  • Default arguments
  • func_get_args()

🔴 Advanced Level


1️⃣1️⃣ What are anonymous functions (closures)?

👉 Functions without name.

$greet = function($name) {
return "Hello $name";
};echo $greet("Aditya");

1️⃣2️⃣ What is a closure in PHP?

👉 Anonymous function that can access variables from outside using use.

$message = "Hello";$func = function() use ($message) {
echo $message;
};$func();

1️⃣3️⃣ What are arrow functions in PHP?

👉 Short syntax (PHP 7.4+)

$sum = fn($a, $b) => $a + $b;

1️⃣4️⃣ What is callable in PHP?

👉 A type that represents valid function calls.

function run(callable $func) {
$func();
}

1️⃣5️⃣ What is function_exists()?

👉 Checks if function is defined.

if (function_exists('test')) {
test();
}

1️⃣6️⃣ What is include vs require in functions?

👉 include → warning if file missing
👉 require → fatal error (script stops)


1️⃣7️⃣ Can a function return multiple values?

👉 Yes (using arrays)

function getData() {
return [1, 2];
}

1️⃣8️⃣ What are variable scope types in functions?

👉 Types:

  • Local
  • Global
  • Static
function test() {
static $x = 0;
$x++;
echo $x;
}

1️⃣9️⃣ What is recursion base case?

👉 Condition to stop recursion.

if ($n == 0) return 1;

2️⃣0️⃣ What is the use of global keyword?

👉 Access global variables inside function.

$x = 10;function test() {
global $x;
echo $x;
}

🎯 Pro Interview Tips

👉 Interviewers often ask:

  • Difference between echo vs return
  • Pass by reference (&)
  • Closures & arrow functions
  • Recursion logic
  • Scope (global/static)

🚀 Bonus Coding Questions

Try solving:

  1. Write a function to reverse a string
  2. Create a function to find factorial (recursive)
  3. Function to check palindrome
  4. Pass array to function and return sum
  5. Create a closure example
  6. Dynamic calculator using variable functions