Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q4 of 15
PHP - Classes and Objects
What will be the output of this PHP code?
class Test {
    public static $value = 5;
    public static function increment() {
        self::$value++;
    }
}
Test::increment();
Test::increment();
echo Test::$value;
A7
B5
C2
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand static property initial value

    Static property $value starts at 5.
  2. Step 2: Each call to increment() increases value by 1

    Two calls increase value from 5 to 7.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    Static property increments correctly = 7 [OK]
Quick Trick: Static properties keep updated value across static method calls [OK]
Common Mistakes:
  • Assuming static property resets each call
  • Confusing instance and static property increments

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes