PHP Variable Variables

Summary: in this tutorial, you’ll learn about PHP variable variables and how to apply them effectively.

Introduction to the PHP variable variables

Typically, you have a variable with a predefined name. For example, the following defines a variable with the name $title that holds a string.

<?php $title = 'PHP variable variables';
Code language: HTML, XML (xml)

In PHP, the name of a variable can be derived from the value of another variable. For example:

<?php $my_var= 'title'; $$my_var = 'PHP variable variables'; echo $title;
Code language: HTML, XML (xml)

Output:

PHP variable variables

How it works.

  • First, define a variable $my_var that holds the string 'title'.
  • Second, define a variable variable that holds the string 'PHP variable variables'. Note that we use double $ signs instead of one. By doing this, we technically create another variable with the name $title.
  • Third, display the value of the $title variable.

PHP variable variables example

Suppose that you have the following folder and files:

├── inc | └── home.php └── index.php

In the index.php, you define the following view() function that loads the code from a file specified by the $file parameter:

<?php function view(string $file): void { require __DIR__ . $file; }
Code language: HTML, XML (xml)

To pass the data to the script specified by the $file, you can add a second parameter to the view() function like this:

<?php function view(string $file, array $data): void { require __DIR__ . $file; }
Code language: HTML, XML (xml)

From the $file script, you can access the elements of the $data array.

The following example uses the view() function to load the code from the home.php script and pass an array of two elements to it:

<?php function view(string $file, array $data): void { require __DIR__ . '/' . $file; } view( 'inc/home.php', [ 'title' => 'Home', 'heading' => 'Welcome to my homepage' ] );
Code language: HTML, XML (xml)

In the home.php, you can access the $data array like this:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title><?php echo $data['title'] ?>/title> </head> <body> <h1><?php echo $data['heading'] ?></h1> </body> </html>
Code language: HTML, XML (xml)

When you launch the index.php file, it loads the code from the home.php file and displays the title and heading. However, it would be great if you can access the elements of the $data array in the home.php like this:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title><?php echo $title ?>/title> </head> <body> <h1><?php echo $heading ?></h1> </body> </html>
Code language: HTML, XML (xml)

To do that, you need to transform the array elements into variables using the variable variables. The new view() function will look like this:

function view(string $file, array $data): void { foreach ($data as $key => $value) { $$key = $value; } require __DIR__ . '/' . $file; }
Code language: PHP (php)

In the view() function, we iterate over the elements of the $data array and create variables whose names are the keys of the $data array and values are the values of the $data array:

foreach ($data as $key => $value) { $$key = $value; }
Code language: PHP (php)

The variable variables are now available in the script specified by the $file parameter. For example, if you call the view() function to load the code from the home.php file as follows:

view( 'inc/home.php', [ 'title' => 'Home', 'heading' => 'Welcome to my homepage' ] );
Code language: PHP (php)

In the home.php file, you can access the $title and $heading variables:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title><?php echo $title ?>/title> </head> <body> <h1><?php echo $heading ?></h1> </body> </html>
Code language: HTML, XML (xml)

Summary

  • PHP variable variables are variables whose names are set dynamically.
Did you find this tutorial useful?