Dynamic variables
What is a dynamic variable, or a variable variable, as they are sometimes called? When you create a variable, you give it a name. Consider this PHP example:
-
<?php
-
$foo = 'test';
-
echo $foo;
-
?>
The first line will create the variable $foo and assign the string value 'test' to it. In the second line, the content of the variable $foo will be printed. So far, nothing exciting. But now consider this:
-
<?php
-
$bar = 'foo';
-
$$bar = 'test';
-
?>
(Note the double $ sign at the beginning of the second line.) What happens here? First, the value 'foo' is assigned to the variable bar. In the second line, a variable is created, and this variable's name will be the content of the variable $bar. This means, the variable $foo is created. Hence, the third line will output testfoo. You can also have dynamic variables in arrays and objects, even as function aliases:
-
<?php
-
$foobar = $foo[$bar];
-
$foobar = $foo->$bar;
-
-
// want to match case sensitive or not?
-
$stripos_func = ($casesensitive) ? 'strpos' : 'stripos';
-
?>

