Re: How do I write a compound variable?
| From: | Hugh Danaher | Date: | Tue, 07 Aug 2001 06:34:27 +0000 |
| Subject: | Re: How do I write a compound variable? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-61553@lists.php.net to get a copy of this message | ||
Arthur,
Thank you very much for your help. I tried it and it works perfectly.
I haven't tried the array yet though, I'm a bit shy of them as I don't know
much about accessing arrays yet. Perhaps in the future.
Thank you again.
Hugh
----- Original Message -----
From: Arthur <x@apx.org>
To: Hugh Danaher <hdanaher@earthlink.net>
Sent: Monday, August 06, 2001 5:41 PM
Subject: Re: [PHP] How do I write a compound variable?
> To make your code work use curly braces for the compound variables.
>
> http://www.php.net/manual/en/language.variables.variable.php
>
> $high_1995=22;
> $high_1996=23;
> $high_1997=20;
> $high_1998=18;
> $high_1999=20;
> $high_2000=21;
> $high_2001=23;
>
> $low_1995=22;
> $low_1996=23;
> $low_1997=20;
> $low_1998=18;
> $low_1999=20;
> $low_2000=21;
> $low_2001=23;
>
> for ($year=1995;$year<=2001;$year++)
> {
> ${"avg_" . $year} = ( ${"high_" . $year} + ${"low_" . $year}
> )/2; // create $avg_1995, ...
> print "${"avg_" . $year}<br>";
> }
>
>
> To do it without compound variables do this:
>
> $arr = array(
> 1995 => array(high=>22,low=>22),
> 1996 => array(high=>23,low=>23),
> 1997 => array(high=>20,low=>20),
> 1998 => array(high=>18,low=>18),
> 1999 => array(high=>20,low=>20),
> 2000 => array(high=>21,low=>21),
> 2001 => array(high=>23,low=>24),
> );
>
> foreach($arr as $year=>$vals){
> $avg = ( $vals["high"] + $vals["low"] ) / 2;
> echo $year . " - " . $vals["high"] . " - " .
> $vals["low"] . " - " . $avg .
> "<br>\n";
> }
>
>
>
> - Arthur
>