It's time to stop searching a just ask. I can't find an answer online for the life of me. Anyway, I am going through someone else's code and they have this syntax inside of a loop and I'm not sure exactly what is happening.
foreach($params as $key => $val) {
${$key} = $val
}
It's the ${$key} that I don't understand.
This is called variable variables. In your loop, the code will set the variable who's name is $key
to the value $val
.
The loop could be replaced with extract()
.
This essentially does what extract()
does:
$params = array('a' => 'foo', 'b' => 'bar');
foreach($params as $key => $val) {
${$key} = $val
}
echo $a; // outputs 'foo'
echo $b; // outputs 'bar'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With