Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP Variable in a Loop?

Posted on 2009-07-04
6
Medium Priority
?
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 143

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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

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

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
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.
Suggested Courses

610 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