Call By Reference — PHP Technical Interview Questions & Answers

Call By Reference

Call By Reference

  • Call By Reference 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 Call By Reference.
  • From the example next to this theory: Call By Reference: scores array, print avg, then high marks.
  • Trap: no return so the caller gets null. Fix that before you talk about advanced PHP.
  • Viva: what is Call By Reference? Then show function name($x) { return $x; }. Then name the trap.

Technical Interview Tip: What is Call By Reference? Show this: function name($x) { return $x; }. Trap: no return so the caller gets null.

Code Examples for Call By Reference

Call By Reference — working example

<?php
// Call By Reference
$scores = [80, 90, 70];
$avg = array_sum($scores) / count($scores);
echo "avg=" . number_format($avg, 1) . "\n";
foreach ($scores as $s) {
  if ($s >= 80) echo "high: $s\n";
}

Call By Reference: scores array, print avg, then high marks. Say the output.

How to Explain Call By Reference to an Interviewer

What is Call By Reference?

Used for function add($a, $b) a marks helper Not just the heading

Give one small example of Call By Reference.

function name($x) { return $x; } Call By Reference: scores array, print avg, then high marks

What mistake do beginners make with Call By Reference?

no return so the caller gets null Show the fix

Where do you use Call By Reference?

a marks helper function add($a, $b)