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 Animal {
  public $type = "Cat";
}
$pet = new Animal();
echo $pet->type;
AAnimal
Btype
CCat
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand object property access

    The class Animal has a public property type set to "Cat". Creating an object $pet and accessing $pet->type returns "Cat".
  2. Step 2: Predict output

    Echoing $pet->type prints "Cat".
  3. Final Answer:

    Cat -> Option C
  4. Quick Check:

    Accessing public property = property value [OK]
Quick Trick: Use -> to access object properties in PHP [OK]
Common Mistakes:
  • Trying to access property without ->
  • Expecting class name as output
  • Confusing property name with value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes