2

I need to create a php file with a hundred variables, which are all identical except for their id.

PHP Code

$myvar1 = get_input('myvar1');
$myvar2 = get_input('myvar2');
$myvar3 = get_input('myvar3');
$myvar4 = get_input('myvar4');
...
$myvar100 = get_input('myvar100');

I wonder if it is possible to create only one line as a model, and is replicated 100 times?

Thanks.

6
  • 6
    Why don't you use an array? That's exactly what arrays are for, holding a set of values. Commented Feb 29, 2012 at 11:15
  • 1
    Hello Felix, i'm not familiar with array !? Commented Feb 29, 2012 at 11:15
  • 2
    Then have a look at the documentation: php.net/manual/en/language.types.array.php Commented Feb 29, 2012 at 11:16
  • 2
    You can do it, but for moral purposes I personally won't answer how because as @FelixKling said - that's why arrays exists. Commented Feb 29, 2012 at 11:16
  • I could also not see the purpose behind this.... Commented Feb 29, 2012 at 11:18

5 Answers 5

3

Just store it in an array instead of 100 variables:

$myvar = Array();
for ($i = 1; $i <= 100; ++$i) {
    $myvar[$i] = get_input('myvar' . $i);
}

Or if you want the indexes to start at zero:

$myvar = Array();
for ($i = 1; $i <= 100; ++$i) {
    $myvar[$i - 1] = get_input('myvar' . $i);
}
Sign up to request clarification or add additional context in comments.

3 Comments

What have this array done wrong to you have starting index of 1? :(
@jperovic, sorry I don't really understand your question. What do you mean? You can have it so the indexes match the input names (so get_input('myvar', 13) would be stored in $myvar[13]) or you can start the indexes at zero if you don't need them to match the input names.
Oh sorry, if that you done on purpose of matching indexes with myvar that's alright. :)
2

You really must use Arrays for this, its far more appropriate:

$myvar = Array();
for($i=1;$i<101;$i++) $myvar[$i]=get_input("myvar{$i}"); // $myvar[1]=... etc...

Anyway, its possible to create variable names dynamically (notice the '$$'):

for($i=1;$i<101;$i++){
        $varName="myvar{$i}";
        $$varName=get_input($varName);
}

Comments

1

you could store your variables in an array

$myvarArr = array();
for($i=0;i<100;i++) {
    $myvarArr[$i] = get_input('myvar' . ($i+1));
 }

Comments

1

Looks like you just need an array, an array right from the outside variable.

if you make your form field name like this

<input type="text" name="myvar[]">
<input type="text" name="myvar[]">
<input type="text" name="myvar[]">
<input type="text" name="myvar[]">

you can easily get all the values by as simple code as

$myvar = $_POST['myvar'];

1 Comment

I gave this +1, because it's good practical advice that would certainly apply in some cases similar to the OPs, so this will be useful for people coming across this page to see. But note that original poster was getting data from get_input(...), not from $_POST, so this answer might not be applicable in this particular case.
0

Just to show, that it's possible, use variable variables (i just love this php feature):

for ($i = 1; $i <= 100; $i++) {
    $varName = "myvar{$i}"; // create variable name
    $$varName = get_input("myvar{$i}"); // $$varName => $myvar5, when $i == 5 and so on
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.