vendredi 27 novembre 2015

PHP inherit global variable in Anonymous functions

I can't use variable $data in Anonymous functions:

$data = array(...);
Excel::create('Filename',function($excel){
  foreach($data as $v){
     //...
  }
});

I get error: "Undefined variable: $data"

Also:

 $data = array(...);
 Excel::create('Filename',function($excel){
   global $data;
   foreach($data as $v){
      //...
   }
 });

I get error "Invalid argument supplied for foreach()"

How I can use $data in Anonymous function?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire