Dereferencing Variables in PHP

This is a discussion on Dereferencing Variables in PHP within the PHP Language forums, part of the PHP Programming Forums category; I have a set of variables called $topten_1, $topten_2, $topten_3, etc. I'd like to use a for($i = 1 ; $...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-14-2005
Shannon A.
 
Posts: n/a
Default Dereferencing Variables in PHP

I have a set of variables called $topten_1, $topten_2, $topten_3, etc.

I'd like to use a for($i = 1 ; $i<=10 ; $i++) loop to reference them,
but can't quite figure out how to. These don't work:

$topten_$1
${topten_$1}
$"topten_$1"

Is there someway to do this in PHP, or is the only answer to use an
associative array?

Shannon

Reply With Quote
  #2 (permalink)  
Old 05-14-2005
Jerry Stuckle
 
Posts: n/a
Default Re: Dereferencing Variables in PHP

Shannon A. wrote:
> I have a set of variables called $topten_1, $topten_2, $topten_3, etc.
>
> I'd like to use a for($i = 1 ; $i<=10 ; $i++) loop to reference them,
> but can't quite figure out how to. These don't work:
>
> $topten_$1
> ${topten_$1}
> $"topten_$1"
>
> Is there someway to do this in PHP, or is the only answer to use an
> associative array?
>
> Shannon
>


Shannon,

Something like this should work...

$var = 'topten_' . $i;
echo $$var;

But I'd use an array. Much cleaner code and easier to do. Since you're
using integer indicies anyway, it doesn't have to be associative.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
  #3 (permalink)  
Old 05-15-2005
Norman Peelman
 
Posts: n/a
Default Re: Dereferencing Variables in PHP

"Shannon A." <appel@erzo.org> wrote in message
news:1116096102.753079.138950@g44g2000cwa.googlegr oups.com...
> I have a set of variables called $topten_1, $topten_2, $topten_3, etc.
>
> I'd like to use a for($i = 1 ; $i<=10 ; $i++) loop to reference them,
> but can't quite figure out how to. These don't work:
>
> $topten_$1
> ${topten_$1}
> $"topten_$1"
>
> Is there someway to do this in PHP, or is the only answer to use an
> associative array?
>
> Shannon
>


Close... try:

${"topten_$i"}



Norm
---
FREE Avatar hosting at www.easyavatar.com


Reply With Quote
  #4 (permalink)  
Old 05-15-2005
Oli Filth
 
Posts: n/a
Default Re: Dereferencing Variables in PHP

Shannon A. wrote:
> I have a set of variables called $topten_1, $topten_2, $topten_3,

etc.
>
> I'd like to use a for($i = 1 ; $i<=10 ; $i++) loop to reference them,
> but can't quite figure out how to. These don't work:
>
> $topten_$1
> ${topten_$1}
> $"topten_$1"


Don't use this approach, it's just a complete pain. Use an array, it's
far easier and there's dozens of PHP functions there to help you do
this.

> Is there someway to do this in PHP, or is the only answer to use an
> associative array?


You don't even have to use an associative array, numeric indices will
do the trick, e.g.

$topten[1]
$topten[2]
etc...

--
Oli

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 11:38 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0