Variable Escope Doubt

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
gakkirez
New php-forum User
New php-forum User
Posts: 1
Joined: Thu Mar 29, 2018 5:58 am

Thu Mar 29, 2018 6:04 am

Hi. Before posting , I did a quick search with words: Variable Escope and just Escope, but since there was no matches, here goes my doubt. In the ex, below:

Code: Select all

public function do_api(Request $request) {
  $keyword = $request->input('keyword');
  $str_keyword = '"'.$keyword.'"';
  
  $url = 'http://www.someurl.com/search.do?searchCountry=us';
  $content = file_get_contents($url);
  $json = json_decode($content, true);
  
  $shirts = array_filter($json['searchItemBeanList'],function($item) {
    return stripos($item['itemName'], $str_keyword) !== false;
    // HERE $str_keyword is not defined!
  });
}
How and Where should I declare $str_keyword so I can read it?

chorn
php-forum Fan User
php-forum Fan User
Posts: 562
Joined: Fri Apr 01, 2016 2:18 am

Thu Mar 29, 2018 6:24 am

array_filter expects a closure. Every closure, as every function, has it's own scope, defined variables are provided as argument, or with the USE statement.

Post Reply