Static Variables — PHP Technical Interview Questions & Answers

Static Variables

Static Variables

  • Static Variables is a PHP topic. In plain words you use it for $marks = 72 in a result script. Don’t start with a slogan — start with that picture.
  • Smallest example: $name = 'Asha';. Type it, run it, and say what you see. If you can do that from memory, you know Static Variables.
  • From the example next to this theory: Static Variables: scores array, print avg, then high marks.
  • Trap: using marks without $. Fix that before you talk about advanced PHP.
  • Viva: what is Static Variables? Then show $name = 'Asha';. Then name the trap.

Technical Interview Tip: What is Static Variables? Show this: $name = 'Asha';. Trap: using marks without $.

Code Examples for Static Variables

Static Variables — working example

<?php
// Static Variables
$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";
}

Static Variables: scores array, print avg, then high marks. Say the output.

How to Explain Static Variables to an Interviewer

What is Static Variables?

Used for $marks = 72 a result script Not just the heading

Give one small example of Static Variables.

$name = 'Asha'; Static Variables: scores array, print avg, then high marks

What mistake do beginners make with Static Variables?

using marks without $ Show the fix

Where do you use Static Variables?

a result script $marks = 72