?
Solved

PHP Variable in a Loop?

Posted on 2009-07-04
6
Medium Priority
?
418 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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

762 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