-3

why the variable $ replaced changed to array in a function ?

foreach ($rowset as $row)
{
    $replaced = str_replace('$uid',$this->bbcode_uid,$row['first_pass_replace']);
    echo $replaced; // this is a test of the variable error and it's ok
    $this->bbcodes[$row['bbcode_tag']] = array(
        'bbcode_id' => (int) $row['bbcode_id'],
        'regexp'    => array($row['first_pass_match'] => function($replaced){
                return $replaced; //this is not working 
            }
            )
        );
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • It's not clear what you're trying to do, but you seem to be trying to assign a function as an element of an array. It's very unlikely that's really what you want to do. But in any case, even if it *is* what you want to do, the parameter `$replaced` has local scope, limited to that function; it has nothing to do with the variable named `$replaced` that you are using *outside* that function. – elixenide Jun 26 '18 at 18:18
  • Try to look at scope of variables in php – Zyigh Jun 26 '18 at 18:20
  • the problem is I was debugging error of /e modifier is deprecated ,use preg_replace_callback() instead inside message_parser.php in a phpbb forum so I had to provide the callback function (replacement) as the second element of array that is used by the main preg_replace_callback () function to output transformed bbcode to html again in the post preview page – Abdallah Jun 26 '18 at 21:52

1 Answers1

0

I honestly have no idea what you are trying to accomplish but to make it "work" you need:

...
'regexp' => array(
    $row['first_pass_match'] => function() use($replaced) {
        return $replaced;
    }
)
...

You use use() to achieve closure and bring an external variable into the function without explicitly passing it in as a parameter.

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77