On 15 June 2004 08:25, Ulrik S. Kofod wrote:

> [EMAIL PROTECTED] sagde:
> > 
> > Hi,
> > I have variables called Cookie1 to Cookie35.
> > 
> > I would like to print the values of  Cookie1 to Cookie35 using for
> > loop. 
> > 
> > Could anybody correct the below code to print the variables.
> > 
> > =============================for($i=1;$i<34;$i++)
> > {
> >     $x="Cookie".$i;
> > 
> >     if(isset($$x))
> >     {
> >             echo "<p>$x:$$x</p>";
> >     }
> > }
> 
> 
> for($i=1;$i<34;$i++)
> {
>         $x="Cookie".$i;
>         eval("\$y = \$$x;");
>         if(isset($y))
>         {
>                 echo "<p>$x:$y</p>";
>         }
> }

No need for eval() here, this will work just fine:

    $x = "Cookie$i";
    $y = $$x;

You could also collapse the two statements like this (if you didn't need
$x):

    $y = ${"Cookie$i"};
or
    $y = ${"Cookie".$i};

However, this seems to be a classic case where the use of variable variables
looks like a fudged solution, and you should examine your data to see if it
can't be better represented in an array (which would have made coding the
above loop a snap).

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to