0

I'm learning PHP and I encounter this piece of code:

$args = [
    "msg" => "hello world!"
];

foreach ($args as $key => $value) {
    $$key = $value;
}

echo $msg; // prints hello world!

Why can I acces $msg if it was created inside the for loop? Shouldn't it be out of context?

DiBV
  • 11
  • 3
  • I don't know why this question was closed, and it's not dubplicated – eladcm Mar 12 '21 at 09:28
  • it is Variable Variable , eg double $$, which creates the $msg from the foreach loop – eladcm Mar 12 '21 at 09:29
  • 1
    Nope, it shouldn't be out of context, because control structures don't have their own scope. They're using script scope. @eladcm It was closed because the target questions have the full explanation for this behaviour. There is no need to repeat the same things over again. – El_Vanja Mar 12 '21 at 10:13

0 Answers0