Bird
0
0

What will be the output of the following PHP code?

medium📝 Predict Output Q4 of 15
PHP - Classes and Objects
What will be the output of the following PHP code?
class Car {
  public $color;
  public function __construct($color) {
    $this->color = $color;
  }
}
$myCar = new Car('red');
echo $myCar->color;
Ared
BCar
Ccolor
DError
Step-by-Step Solution
Solution:
  1. Step 1: Analyze constructor parameter and property assignment

    The constructor sets the object's color property to the passed argument 'red'.
  2. Step 2: Check the echo statement output

    Echoing $myCar->color prints the string 'red' stored in the property.
  3. Final Answer:

    red -> Option A
  4. Quick Check:

    Constructor sets property = output 'red' [OK]
Quick Trick: Constructor assigns property, echo prints assigned value [OK]
Common Mistakes:
  • Expecting class name instead of property value
  • Confusing property name with value
  • Thinking code causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes