The PHP Double Cash

Dynamic variable is one of those things in PHP where it was hard for me to see its application. The idea behind it is not too bad. Let's assume that you have a string value assigned to a variable $str. Adding another $ to $str will dynamically create a variable with the name equivalent to the string value assigned to $str. Wuuuaah?!

$str = "name";
$$str = "James Bond";   // ${"name"} = "James Bond";
echo $name;

The above code will output "James Bond". I think the key to understanding this is to recognize that $$str gets evaluated to ${"name"}, thus creating $name.

While this is good and all, how dynamic variables can be useful is still unclear. It was not until recently when I created this blog using the MVC pattern that I first saw the application of dynamic variables.

$data = [

foreach($data as $key => $value) {
    $$key = $value;


You will now have access to the $id, $firstname, and $lastname variables along with their corresponding values in the file where you include the index.php. In this situation, dynamic variables seem to serve as refactor and increase legibility.

This article is my 2nd oldest. It is 202 words long, and it’s got 0 comments for now.