Bird
Raised Fist0
PHPprogramming~20 mins

Instanceof operator in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Instanceof Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PHP code using instanceof?

Consider the following PHP code snippet. What will it output?

PHP
<?php
class Animal {}
class Dog extends Animal {}
$dog = new Dog();
if ($dog instanceof Animal) {
    echo 'Yes';
} else {
    echo 'No';
}
?>
ANo
BYes
CFatal error
Dnull
Attempts:
2 left
💡 Hint

Remember that instanceof checks if an object is an instance of a class or its subclass.

❓ Predict Output
intermediate
2:00remaining
What does this code output when checking interface with instanceof?

Given this PHP code, what will be printed?

PHP
<?php
interface Flyer {}
class Bird implements Flyer {}
$bird = new Bird();
if ($bird instanceof Flyer) {
    echo 'Can fly';
} else {
    echo 'Cannot fly';
}
?>
ACan fly
BNo output
CFatal error
DCannot fly
Attempts:
2 left
💡 Hint

Check if instanceof works with interfaces as well as classes.

❓ Predict Output
advanced
2:00remaining
What error or output does this code produce?

Analyze this PHP code and select the correct output or error message.

PHP
<?php
class Vehicle {}
$car = new Vehicle();
if ($car instanceof NonExistentClass) {
    echo 'Yes';
} else {
    echo 'No';
}
?>
AYes
BWarning: instanceof expects an object
CFatal error: Class 'NonExistentClass' not found
DNo
Attempts:
2 left
💡 Hint

What happens if you use instanceof with a class name that does not exist?

❓ Predict Output
advanced
2:00remaining
What is the output of this code using instanceof with null?

What will this PHP code print?

PHP
<?php
class Person {}
$person = null;
if ($person instanceof Person) {
    echo 'Is a person';
} else {
    echo 'Not a person';
}
?>
ANot a person
BIs a person
CFatal error
DWarning: instanceof expects an object
Attempts:
2 left
💡 Hint

Check how instanceof behaves when the variable is null.

🧠 Conceptual
expert
2:00remaining
Which statement about the instanceof operator in PHP is TRUE?

Select the only true statement about the instanceof operator in PHP.

Ainstanceof can be used to check if a variable is a primitive type like integer or string.
Binstanceof returns true if the variable is null and the class name matches any class.
Cinstanceof can be used to check if a variable is an instance of a class or interface, even if the class or interface does not exist.
Dinstanceof throws a fatal error if the class or interface name used does not exist in the code.
Attempts:
2 left
💡 Hint

Think about what happens if you use instanceof with a class name that PHP does not know.

Practice

(1/5)
1. What does the instanceof operator do in PHP?
easy
A. Creates a new instance of a class
B. Checks if an object is an instance of a specific class or its subclass
C. Deletes an object from memory
D. Converts an object to a string

Solution

  1. Step 1: Understand the purpose of instanceof

    The instanceof operator is used to check if a variable is an object of a certain class or subclass.
  2. Step 2: Differentiate from other operations

    It does not create, delete, or convert objects; it only checks their type.
  3. Final Answer:

    Checks if an object is an instance of a specific class or its subclass -> Option B
  4. Quick Check:

    instanceof checks object type [OK]
Hint: Remember: instanceof checks object type, not creation or deletion [OK]
Common Mistakes:
  • Confusing instanceof with object creation
  • Thinking instanceof deletes objects
  • Assuming instanceof converts objects to strings
2. Which of the following is the correct syntax to check if $obj is an instance of class Car?
easy
A. if ($obj instance Car) {}
B. if ($obj is Car) {}
C. if (instanceof $obj Car) {}
D. if ($obj instanceof Car) {}

Solution

  1. Step 1: Recall correct instanceof syntax

    The correct syntax is: variable instanceof ClassName, without extra keywords or rearrangement.
  2. Step 2: Check each option

    if ($obj instanceof Car) {} matches the correct syntax exactly. Others have wrong keywords or order.
  3. Final Answer:

    if ($obj instanceof Car) {} -> Option D
  4. Quick Check:

    Correct instanceof syntax [OK]
Hint: Syntax is always: variable instanceof ClassName [OK]
Common Mistakes:
  • Using 'is' instead of 'instanceof'
  • Reversing order of variable and class
  • Adding extra keywords inside condition
3. What will be the output of the following PHP code?
$a = new DateTime();
if ($a instanceof DateTime) {
    echo 'Yes';
} else {
    echo 'No';
}
medium
A. Yes
B. No
C. Error
D. Nothing

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]
Hint: 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
4. Identify the error in this PHP code snippet:
class Animal {}
class Dog extends Animal {}
$dog = new Dog();
if ($dog instanceof 'Animal') {
    echo 'It is an animal';
}
medium
A. Missing semicolon after class definition
B. Incorrect object creation syntax
C. Using quotes around class name in instanceof
D. Using instanceof with a variable instead of object

Solution

  1. Step 1: Check instanceof usage

    The instanceof operator expects a class name without quotes, not a string.
  2. Step 2: Verify other parts

    Classes and object creation are correct; semicolons are present.
  3. Final Answer:

    Using quotes around class name in instanceof -> Option C
  4. Quick Check:

    Class name must be unquoted in instanceof [OK]
Hint: Do not put quotes around class names in instanceof [OK]
Common Mistakes:
  • Putting class names in quotes inside instanceof
  • Confusing instanceof with string comparison
  • Missing semicolons elsewhere (not in this code)
5. Given the classes below, what will be the output?
class Vehicle {}
class Car extends Vehicle {}
class Bike extends Vehicle {}

function checkVehicle($obj) {
    if ($obj instanceof Car) {
        return 'Car';
    } elseif ($obj instanceof Vehicle) {
        return 'Vehicle';
    } else {
        return 'Unknown';
    }
}

$myBike = new Bike();
echo checkVehicle($myBike);
hard
A. Vehicle
B. Unknown
C. Car
D. Error

Solution

  1. Step 1: Analyze object type

    $myBike is an instance of Bike, which extends Vehicle.
  2. Step 2: Follow function logic

    First condition checks if $obj is Car - false. Second checks if $obj is Vehicle - true because Bike is subclass of Vehicle.
  3. Final Answer:

    Vehicle -> Option A
  4. Quick Check:

    Bike is a Vehicle, not Car = Vehicle [OK]
Hint: Check subclass before superclass in instanceof chains [OK]
Common Mistakes:
  • Expecting 'Car' for Bike object
  • Ignoring subclass relationship in instanceof
  • Assuming instanceof only matches exact class
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard