GreyWyvern wrote:
> On Thu, 18 Sep 2003 01:52:59 +0000 (UTC), Daniel Ruscoe
> <contact.TakeThisOut@website.plz> wrote:
>
>> Here's a confusing one.
>>
>> I want to feed a PHP script a variable value that will be used as part
>> of a variable name when the script runs.
>>
>> $1a = "Something";
>> $1b = "Something else";
>> $1c = "Something else entirely";
>>
>> // Run /danielsscript?code=b
>>
>> print (" Blah blah blah: $1$code "); // Variables somehow merge as one
>>
>> // Result: "Blah blah blah: Something else"
>>
>> Any idea on how to manage it? Thanks!
>
>
> Just use a text-keyed array. Instead of:
>
> $1a = "Something";
> $1b = "Something else";
> $1c = "Something else entirely";
>
> use:
>
> $txt['a'] = "Something";
> $txt['b'] = "Something else";
> $txt['c'] = "Something else entirely";
>
> // Run /danielsscript?code=b
>
> print (" Blah blah blah: {$txt[$code]}"); // $code is the key to the
> requested array value.
>
> // Result: "Blah blah blah: Something else"
>
> Easy peasy. BTW, these kinds of questions are better asked in
<font color=purple> > <a style='text-decoration: underline;' href="news://comp.lang.php</font" target="_blank">news://comp.lang.php</font</a>>
Yes, 'tis the easy way.
I think you could also get the same effect using syntax similar to Dan's
(for the sake of argument + alternative ways of doing things + to show off):
$1a = "Something";
$1b = "Something else";
$1c = "Something else entirely";
// Run /danielsscript?code=b
print (" Blah blah blah: ${"1" . $code} "); // Variables somehow merge
as one
// Result: "Blah blah blah: Something else"
Should do the trick.
Alternatively you could have
$code = "1" . $code;
// and
print (" Blah blah blah: $$code "); // Variables somehow merge as one
Anyhow, <a style='text-decoration: underline;' href="http://au2.php.net/variables.variable" target="_blank">http://au2.php.net/variables.variable</a> is a fairly good reference
on how to generate variables on the fly.
</2c><!-- ~MESSAGE_AFTER~ -->
>> Stay informed about: PHP: Variable as part of a variable