Variable variable is not accessible within callback scope while using arrow function syntax (Warning: Undefined variable)
Asked Answered
F

2

1

I need to use a variable variable for defining a lambda function inside of the callback of array_map(). I get an "Undefined variable" WARNING for the variable that I am referencing. Subsequently, I do not get the expected result. The expected and actual results are commented within the code below. A PHP Sandbox of the problem is also given.

$a = "hello";
$$a = "world";

# Prints "world" as expected.
echo $hello;

# GIVES WARNING !!!! (Undefined variable $hello)
$res = array_map(fn($number) => $number . $$a, ["1", "2"]);

# Expected: ["1 world", "2 world"]
# Actual: ["1", "2"]
echo var_dump($res);

PHP Sandbox

Tried:

  1. Replacing $$a with ${$a} did not fix the problem.
  2. Replacing $$a with $hello fixes the problem BUT that solution is not applicable in my particular use case.
  3. I have tried using other functions instead of the concatenation (fn($number) => func($number, $$a)) and the problem remains.
Fulmer answered 23/2 at 10:15 Comment(0)
S
2

Your issue is due to scoping rules. $$a is not in the local scope of the function, while $world is a regular variable that you've defined in the same scope as your array_map function, so it's recognized and can be used inside the function.

$a = "hello";
$$a = "world";
$world = $$a;

$res = array_map(fn($number) => $number . $world, ["1", "2"]);

var_dump($res);
Shuster answered 23/2 at 10:56 Comment(0)
B
1

Variables $$ don't behave the same way as they do in regular PHP code inside an arrow function. Here is documentation with explanations and examples; This is why you're getting Undefined variable warning. You can use a regular anonymous function, which has access to the variable $$, instead of an arrow function

$a = "hello";
$$a = "world";

// Assigning the variables $$ to a new variable for use in the closure
$world = ${$a};

$res = array_map(function($number) use ($a, $world) {
    return $number . $world;
}, ["1", "2"]);

var_dump($res);

The keyword use brings the variable $a into the scope of the anonymous function so that it can be used inside the function

Basal answered 23/2 at 12:58 Comment(2)
This does not solve the problem. PHP SandboxFulmer
Yes you are right, the variables $$ can't be directly passed through the use statement in a closure. The code is updated, Thank you for alerting me!Basal

© 2022 - 2024 — McMap. All rights reserved.