Solved

PHP Variable in a Loop?

Posted on 2009-07-04
6
417 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 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
The viewer will learn how to count occurrences of each item in an array.
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…

688 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