FriendsOfPHP / Goutte

Goutte, a simple PHP Web Scraper
MIT License
9.26k stars 1.01k forks source link

Variable is null #321

Closed Takuaraa closed 7 years ago

Takuaraa commented 7 years ago

Hello,

I'm having a problem. This is an example of my code:


$links = array();
  $test = "OK";

  $client = new Client();

  $crawler = $client->request('GET', $url);
  echo $test;
  $links = $crawler->filter('a')->each(function ($node){
    echo $test;
  });

The first echo $test works, the second one not. It seems that inside the filter.. $test is null. Why is that? How can I fix that?

Thank you.

larowlan commented 7 years ago

Please see http://php.net/manual/en/functions.anonymous.php

You need

 $links = $crawler->filter('a')->each(function ($node) use ($test) {
    echo $test;
  });