Bird
0
0

What will be the output of the following PHP code?

medium📝 Predict Output Q13 of 15
PHP - Inheritance and Polymorphism
What will be the output of the following PHP code?
$a = new DateTime();
if ($a instanceof DateTime) {
    echo 'Yes';
} else {
    echo 'No';
}
AYes
BNo
CError
DNothing
Step-by-Step Solution
Solution:
  1. Step 1: Understand object creation

    $a is created as a new instance of the DateTime class.
  2. Step 2: Check instanceof condition

    The condition $a instanceof DateTime is true because $a is exactly a DateTime object.
  3. Final Answer:

    Yes -> Option A
  4. Quick Check:

    Object is DateTime = Yes [OK]
Quick Trick: If object matches class, instanceof returns true [OK]
Common Mistakes:
  • Assuming instanceof returns false for exact class
  • Expecting syntax error from instanceof
  • Ignoring else branch output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes