Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

deanclarke55's avatar

Cannot figure out Undefined Variable error while following PHP Practitioner 101

Hi, im new to PHP and Im going through the PHP Practitioner 101 class, Im up to lesson 12 - Classes. So far everything makes so much sense and Jeffrey explains things so easy, but Im stuck and I cannot figure out where Im going wrong. Im getting these errors:


Warning: Undefined variable $description in C:\xampp\htdocs\phptest\index.view.php on line 12

Warning: Undefined property: Task::$ in C:\xampp\htdocs\phptest\index.view.php on line 12

Warning: Undefined variable $description in C:\xampp\htdocs\phptest\index.view.php on line 12

Warning: Undefined property: Task::$ in C:\xampp\htdocs\phptest\index.view.php on line 12

Warning: Undefined variable $description in C:\xampp\htdocs\phptest\index.view.php on line 12

Warning: Undefined property: Task::$ in C:\xampp\htdocs\phptest\index.view.php on line 12

Here are my 2 pages of code:

index.php

<?php

class Task {  

    public $description;
    public $completed = false; 
    
    public function __construct($description)  
    {
      $this->description = $description;
    } 

    public  function complete() 
    {
        $this->completed = true;
    }

    public function isComplete()
    {
        return $this->completed;
    }
    
    } 
    
    $tasks = [
        new Task('Go to store'),
        new Task('Pick up milk'),
        new Task('Pay for milk')
    ];

   require 'index.view.php';

index.view.php

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">

    <title>PHP Test</title>
  </head>
  <body>
    <div>
      <ul>
        <?php foreach ($tasks as $task) : ?>
              <li><?= $task->$description; ?></li>
        <?php endforeach; ?> 
      </ul>
    </div>
      
  </body>
</html>

Hope you can help, thanks.

0 likes
5 replies
tykus's avatar
tykus
Best Answer
Level 104

No $ on description here; you are getting the description property on the Task object

<?= $task->description; ?>
1 like
lolsokje's avatar

When accessing a class member, you don't need the $. Instead of $task->$description, use $task->description.

1 like
tykus's avatar

@deanclarke55 no worries, please mark the thread closed by marking a Best Reply above.

Aside, just to knock your socks off; $task->$description is actually valid PHP. Providing there is a $description variable in scope, and its value is a string that matches a property on the Task instance, it will work!

<?php $description = 'description';
<?php foreach ($tasks as $task) : ?>
  <li><?= $task->$description; ?></li>
<?php endforeach; ?> 
deanclarke55's avatar

@tykus Thank you, I think I understand that. Im looking forward to more problems like this - its all part of the fun and the frustrating problems are the ones that stick in my head more so I dont do it again...but I probably will. Thanks again and for being so quick.

Please or to participate in this conversation.