# Undefined variable in block

**URL:** <https://forum.getkirby.com/t/undefined-variable-in-block/20581>\
**Category:** Questions\
**Tags:** v3\
**Created:** [December 30, 2020, 2:47pm UTC](https://forum.getkirby.com/t/undefined-variable-in-block/20581 "2020-12-30T14:47:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sigi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/sigi/32/9998_2.png) [@sigi](https://forum.getkirby.com/u/sigi)\
**Post date:** [December 30, 2020, 2:47pm UTC](https://forum.getkirby.com/t/undefined-variable-in-block/20581/1 "2020-12-30T14:47:59Z")

</div>

Hi there,

it’s probably a lame question but I can’t find the mistake in my php code, so maybe someone sees it.

I defined an own block type and this is the code:

```
<section class="schedule">
  <?php 
  $events = $page->events()->toStructure()->filter(function ($child) {
    return $child->date()->toDate() > time();
  });
  $months = array();
  foreach ($events as $event) :
    $months[] = strftime('%b %Y', strtotime($event->date()));
  endforeach;
  $filteredmonths = array_unique($months);
  foreach ( $filteredmonths as $month) : ?>
    <div class="headline">
      <h2><?= $month ?></h2>
    </div>
    <div class="grid">
      <?php
      $monthevents = $events->filter(function ($child) {
        return strftime('%b %Y', strtotime($child->date())) == $month;
      });
     foreach ($monthevents as $event) : ?>
       <div class="event">
         <span><?= $event->date() ?></span>
         <h3><?= $event->heading() ?></h3>
         <a href="<?= $event->url() ?>">More info <?php snippet('angleright') ?></a>
       </div>
     <?php endforeach ?>
    </div>
  <?php endforeach ?>
</section>

```

And I get the error:  
`Block error: "Undefined variable: monthnow" in block type: "schedule"`

I already found out that the problematic $month variable is the one used inside the filter function, but I don’t know why I cannot use it there but use it above.

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [December 30, 2020, 3:43pm UTC](https://forum.getkirby.com/t/undefined-variable-in-block/20581/2 "2020-12-30T15:43:29Z")

</div>

You have to use the `use` keyword to pass the variable to the callable, because the outer variable is not known inside.

```auto
  $monthevents = $events->filter(function ($child) use($month) {
        return strftime('%b %Y', strtotime($child->date())) == $month;
      });

```

---

<div class="post-metadata">

**Author:** ![sigi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/sigi/32/9998_2.png) [@sigi](https://forum.getkirby.com/u/sigi)\
**Post date:** [December 30, 2020, 3:47pm UTC](https://forum.getkirby.com/t/undefined-variable-in-block/20581/3 "2020-12-30T15:47:08Z")

</div>

Now it works. Thank you very much @texnixe as always!
