Creating Variables Variables

A variable variable takes the value of a variable and treats that as the name of a variable.

<html>
<head>
<title>Creating Variables Variables</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php

$helloworld = "Hello World";
$world = "helloworld";

echo "The variable world has a value $world,
which becomes a new variable $world. <br /><br />";
echo "<font size=+2 color='blue'>${$world}</font><hr />";
echo "This is another way of creating a variable variable. <br /><br />";

$hello = "Hello";
$$hello = "World";

echo "The variable hello has a value $hello,
which becomes a new variable $hello. <br /><br />";
echo "<font size=+2 color='blue'>$hello ${$hello}</font>";
?>
</p>
</body>
</html>

View the effect


Variables | Identifiers | What are Variables? | Assigning Values to Variables | Data Types | Variable Scope | Interpolating Variables in Strings | Creating Variables Variables | Creating Constants
© 2008: Hann So
email: hso@voyager.deanza.edu