Solved

PHP Variable in a Loop?

Posted on 2009-07-04
6
412 Views
Last Modified: 2013-12-13
Is it possible to create 100's of variables with a loop?  The loop I'm using really cuts down on the code in the page, but I lose all my variables that I need to reference.  Here is what I was thinking.

if $variable < 365 {
$variablenumber = 1;
$variablename{$$variablenumber} = (variablenumber + 1);
} else {
echo "all done";
}

Is this possible?

So, the result would be these answers:
$variablename1 = 1
$variablename2 = 2
$variablename3 = 3
...
$variablename365 = 365
0
Comment
Question by:bschwarting
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24777466
>Is it possible to create 100's of variables with a loop?
why not a plain array?

$variablename = array();
for ($i=1; $i<366; $i++)
{
  $variablename[$i] = $i;
} 
//to show the results_
print_r($variablename);

Open in new window

0
 
LVL 12

Expert Comment

by:kevin_u
ID: 24777480
I assume you've looked at an array... $var[$x].

But this should do what you wanted:
$GLOALS['varname'.$variablenumber] = $variablenumber;

you'll get $varname1 = 1, $varname2 = 2,  
and they are all globals.

http://us3.php.net/manual/en/reserved.variables.globals.php



0
 
LVL 19

Accepted Solution

by:
Michael701 earned 500 total points
ID: 24777492
try this

for ($variablenumber=1; $variablenumber<=365 ;$variablenumber++)

{

  $variablename = 'variablename'.$variablenumber;

  $$variablename = $variablenumber;

}
 

echo "var50=".$variablename50 ."<br />\n";

echo "var72=".$variablename72 ."<br />\n";

echo "var321=".$variablename321 ."<br />\n";

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:bschwarting
ID: 24777786
I could only get Michaels701 to work.  Thanks all!
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24777789
yes, that code will do what you asked.

however, still curious about the why you need that in your code?
it really looks like you are doing something wrong in your method...
0
 
LVL 1

Author Comment

by:bschwarting
ID: 24777802
angel, I wouldn't doubt it.  I'm just past a beginner on PHP.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now