Bird
0
0

Why does a variable declared inside a PHP function without global or static lose its value after the function ends?

hard📝 Conceptual Q10 of 15
PHP - Functions
Why does a variable declared inside a PHP function without global or static lose its value after the function ends?
ABecause it has local scope limited to the function execution
BBecause PHP automatically deletes all variables after function call
CBecause it is stored in a temporary cache that clears
DBecause it is treated as a constant
Step-by-Step Solution
Solution:
  1. Step 1: Understand variable scope rules

    Variables declared inside functions without global or static have local scope limited to the function execution.
  2. Step 2: Explain why value is lost after function ends

    After function ends, local variables are destroyed and their values lost.
  3. Final Answer:

    Because it has local scope limited to the function execution -> Option A
  4. Quick Check:

    Local scope means variable lifetime is function call only [OK]
Quick Trick: Local variables exist only during function execution [OK]
Common Mistakes:
  • Thinking PHP deletes all variables globally after function
  • Confusing local variables with constants

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes