Dynamic variables in PHP

Access dynamic variables by wrapping {} brackets around, like this:

$var = ${'hello'.$i.'_world'};

If the value of $i is “foo”, then $var would actually be assigned to the value of $hellofoo_world.

This can also be used in arrays (or objects generated from JSON data), like this:

$data = json_decode($output);
$var = $data->{'foo'.$i.'bar'};

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *