Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - How Can I Create Variable Names Within a Loop?

Tags:

variables

php

My goal is to have a loop where variable names are created. Such as:

$variable1
$variable2
$variable3

and assigned values within the loop. For instance, a for loop from 1 to 7 produces the variables $variable1, $variable2, $variable3, .etc, each with the value of the iterator.

like image 694
Jon Lachonis Avatar asked Jun 16 '13 16:06

Jon Lachonis


3 Answers

There are several ways. One is via a string and a double $, but I don't like this method. A programmer could easily remove the double $ (seeing it as a typeo).

for($i = 0; $i <= 7; $i++) {
     $name = "variable{$i}";
     $$name = "foo";
}

The best approach is the explicit statement.

for($i = 0; $i <= 7; $i++) {
     ${"variable$i"} = "foo";
}

With the explicit inline string there is no doubt about the intent of what you're doing. Another PHP programmer will not alter the code by mistake.

like image 116
Reactgular Avatar answered Oct 23 '22 02:10

Reactgular


You should use an array for this:

for($i = 1; $i <= 7; $i++) {
  $variable[$i] = $i;
}

Edit: to clarify, you should use an array because there's no code (to my knowledge) which will accept $variable1 but not $variable[1]. Dynamically generating variable names is all kinds of wrong.

like image 43
kvsm Avatar answered Oct 23 '22 03:10

kvsm


Just to expand on what others have said about why this is a technique best avoided: variable names exist solely for use by the programmer; they have no relationships or properties according to the language's runtime. As such, a collection of related variables should be put into a variable representing some appropriate container - in the case of PHP, its incredibly flexible "array" type.

Whenever the issue of dynamically named variables is raised, it is usually because somebody thinks it is a solution to a particular problem, but it generally leads to more problems than it solves - for instance, once part of a program starts dynamically naming variables, everywhere that wants to use those variables now also has to dynamically name them; finding out which items are actually set becomes unnecessarily complex, and so on.

There are a very few contexts where variable-variables are useful, for certain kinds of debugging and meta-programming, for example, but like goto, their presence in the language should not mean you don't try every other possible solution first.

999 times out of 1000, though, if you find yourself wondering how to dynamically create variable names, you should reframe the question: what are you actually trying to achieve, and what is the best solution to that problem. If some existing / 3rd-party code has been built using this pattern (and presumably also global variables), it may be best to patch or wrap it rather than propogating the mis-design into your own code. (Admittedly, a wrapper will still need to know how to declare the dynamic variables.)

like image 32
IMSoP Avatar answered Oct 23 '22 02:10

IMSoP