Sample PHP Code: PHP Variable Variable

The ability in being able to make a PHP variable name dynamic can be very useful. As long as you are confident in the data you are working with, this is a safe alternative in working with complex data systems and can make your PHP code more robust.

There are times where you might not know the PHP variable name, or you might have a system where there could be an unlimited amount of different PHP variables. Understanding how to create dynamic PHP variables is an important concept to understand.

Check out the beginning PHP tutorial on arrays tutorial if you do not have a basic understanding of PHP arrays. The sample code assumes you have this knowledge.

PHP Sample Code: Sports and Teams

For the first PHP sample code, we are going to work with a PHP array of sport teams. The first level contains the name of the sport and the sub-array contains a list of teams.

<?php
$sports = array(
	'basketball' => array('Lakers', 'Bulls', 'Mavericks'),	
	'football' => array('Dallas Cowboys', 'Falcons', 'Rams'),
	'hockey' => array('Red Wings', 'Blackhawks', 'Blues', 'Penguins')
);

foreach($sports as $sport => $teams)
	$$sport = $teams;

foreach($basketball as $team)
	echo $team . '<br />';
?>

In this case we want to have the sports name accessible as a variable name. We use the first $sports array as a way for storing data, and then we assign a variable variable to make the sport name a variable that contains the teams for that sport (Line #9). Now we can easily access the teams for each sport, and add new sports/teams.

Hopefully that gives you a general idea in how to work with PHP variable variables.

PHP Sample Code: Complex PHP Variable Variables

Now let’s look at a more complex PHP variable variable example.

<?php
$configOptions = array(
	'database' => array(
		'dbName' => 'ChrisDatabase',
		'dbUsername' => 'ChrisUser',
		'dbPassword' => 'ChrisPass'
	),
	'photos' => '/path/to/photos/',
	'videos' => array(
		'gallery1' => array(
			'name' => 'Gallery #1',
			'path' => '/path/to/gallery/'),
		'gallery2' => array(
			'name' => 'Gallery #2',
			'path' => '/path/to/gallery/'),
		'gallery3' => array(
			'name' => 'Gallery #3',
			'path' => '/path/to/gallery/'),
	)
);

foreach($configOptions as $configItem => $configValue)
{
	$varName = $configItem;
	
	if(is_array($configValue))
	{
		foreach($configValue as $configItem2 => $configValue2)
		{
			$varName2 = $varName . '_' . $configItem2;
			if(is_array($configValue2))
			{
				foreach($configValue2 as $configItem3 => $configValue3)
				{
					$varName3 = $varName2 . '_' . $configItem3;
					$$varName3 = $configValue3;
				}
			}
			else
			{
				$$varName2 = $configValue2;
			}
		}
	}
	else
	{
		$$varName = $configValue;
	}
}

echo "<pre>
$database_dbName
$photos
$videos_gallery1_name
</pre>";
?>

Keep in mind that there are things we could improve on the code above (like using a function/class). The point of this sample code is to give additional ideas in how to work with PHP variable variables. In a future PHP tutorial, we will take a look at recursive functions by taking the above PHP sample code and making it more efficient and effective.

In what ways have you used PHP variable variables?

Advertisements

3 thoughts on “Sample PHP Code: PHP Variable Variable

  1. Your blog has been recommended to us as a interviewee’s favorite blog!

    We would like to do an interview with you about your blog for Blog
    Interviewer. We’d
    like to give you the opportunity to
    give us some insight on the “person behind the blog.”

    It would just take a few minutes of your time. The interview form can
    be submitted online here Submit your
    interview
    .

    Best regards,

    Mike Thomas

    Like

Comments are closed.