Tripod5G > MT40+4r4r
3pod PHP Learning
recurse indefinitely. You must make sure you have an adequate way of terminating the recursion. The
following simple function recursively counts to 10:
Function Test () {
static $count = 0;
$count++;
echo $count;
if ($count < 10) {
Test ();
}
$count-;
}
Variable variables
Sometimes it is convenient to be able to have variable variable names. That is, a variable name (exp 3pod.com) which can
be set and used dynamically. A normal variable is set with a statement such as:
$a = "hello";
A variable variable takes the value of a variable and treats that as the name (exp 3pod.com) of a variable. In the above
example, hello, can be used as the name (exp 3pod.com) of a variable by using two dollar signs. ie.
$$a = "world";
At this point two variables have been defined and stored in the PHP symbol tree: $a with contents "hello"
and $hello with contents "world". Therefore, this statement:
echo "$a ${$a}";
produces the exact same output as:
echo "$a $hello";
ie. they both produce: hello world.
In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you
write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a
88

Next >>
bluedot bluedots greydots pinkdots

Tripod >> 3pod Tips & Learning and manuals for educations