User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 423,565 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,604 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 443 | Replies: 8 | Solved
Reply
Join Date: Dec 2007
Posts: 355
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Variables in Variables

  #1  
May 9th, 2008
I have a variable in a variable.

I have done this before but it is not working this time.

I have it in a for loop so my code is:

  1. $abc1 = one;
  2. $abc2 = two;
  3. for($i = 0; $i < 2; $i++) {
  4. echo $$abc$i;
  5. }

The out come should be "one two", correct?

The concept is correct? The code is correct? - to the best of my knowledge and references.

Any ideas? Thanks, Regards X.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2008
Location: Swindon, UK
Posts: 117
Reputation: xan is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 20
xan's Avatar
xan xan is offline Offline
Junior Poster

Re: Variables in Variables

  #2  
May 9th, 2008
The best way to do this would probably be an array:
$abc=array("value1","value2");

for($i = 0; $i < 2; $i++) {
echo $abc[$i];
}
Reply With Quote  
Join Date: Dec 2007
Posts: 355
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Variables in Variables

  #3  
May 9th, 2008
Ya true xan but due to many reasons I cant (mysql - retrieving rows then calculations).

Reply With Quote  
Join Date: May 2008
Location: Swindon, UK
Posts: 117
Reputation: xan is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 20
xan's Avatar
xan xan is offline Offline
Junior Poster

Re: Variables in Variables

  #4  
May 9th, 2008
Not sure exactly how variable variables work, but the PHP manual about them is here
Reply With Quote  
Join Date: Dec 2007
Posts: 355
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Variables in Variables

  #5  
May 11th, 2008
So can anyone help me fix my code please?
Last edited by OmniX : May 11th, 2008 at 8:54 pm.
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Variables in Variables

  #6  
May 15th, 2008
Originally Posted by OmniX View Post
I have a variable in a variable.

I have done this before but it is not working this time.

I have it in a for loop so my code is:

  1. $abc1 = one;
  2. $abc2 = two;
  3. for($i = 0; $i < 2; $i++) {
  4. echo $$abc$i;
  5. }

The out come should be "one two", correct?

The concept is correct? The code is correct? - to the best of my knowledge and references.

Any ideas? Thanks, Regards X.


The output will not be one two because $abc1 is one and not $abc0. Try this.
  1. <?php
  2. $abc1 = "one";
  3. $abc2 = "two";
  4. for($i = 1; $i <= 2; $i++) {
  5. echo ${abc.$i}; //value of $i will be concatenated to string abc and then it will consider the whole as a variable
  6. }
  7. ?>
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Dec 2007
Posts: 355
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Variables in Variables

  #7  
May 15th, 2008
Been really busy with this week with work, wont be able to test this until the weekend or next weekend. But if your 120% this will work ill mark this solved and give you credit and test it later.
Last edited by OmniX : May 15th, 2008 at 9:13 am.
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Variables in Variables

  #8  
May 15th, 2008
Heh! I tested it before posting it here and I am 200% sure it will work..
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Dec 2007
Posts: 355
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Variables in Variables

  #9  
May 16th, 2008
done work, it worked for me.

Now off to solve weirdo problems.

Might post them later.

Thanks nav once again
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 6:42 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC