Solved

PHP Variable in a Loop?

Posted on 2009-07-04
6
413 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

867 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

16 Experts available now in Live!

Get 1:1 Help Now