I needed to construct a recursive string of regular expression patterns. Parts of a string may be built from another string which may also be built of other strings. If you're familiar, think of production rules in a context-free grammar. If you're not, then just take for granted this recursive string idea would be useful. I initially decided to build all my patterns into a single array, giving me a nice data collection to access them. I was also under the assumption that my recursive idea would work best with an array as the interpreter would have to account for all strings in the array at once. This would prevent order issues if one string relied on another but wasn't yet declared. Unfortunately, PHP simply doesn't seem to work as I had hoped.
Consider the following snippet of code:
[code lang="php"]<?php
$a = array(
0 => "test {$a[1]}",
1 => "blah {$a[2]}",
2 => "argh"
);
echo $a[0]."\n"; // outputs: test
$s2 = "argh";
$s1 = "blah {$s2}";
$s0 = "test {$s1}";
echo $s0."\n"; // outputs: test blah argh
?>[/code]
At first, we attempt to use an array to handle the string recursion, then we use string variables.We can see from the output that the array code simply doesn't work. It appears you can't access parts of the array from within itself. At the moment, it looks like I'm stuck avoiding arrays for this. If anyone out there knows a way around this, please let me know!