Hey I have some variables that differ just for a single digit, like this:
$variable1 = '';
$variable2 = '';
I was thinking about putting them inside a for loop to number them automatically with the loop index, is this possible? I mean something like this:
for ($i=1; $i<10; $i++) {
$variable$i = '';
}
This of course doesn’t work, so I’ve also tried writing
$variable{$i} = '';
but it doesn’t work either.
http://www.php.net/manual/en/language.variables.variable.php
Be aware that in most cases you will probably end up using an array anyways.
Cups
September 27, 2011, 4:49pm
3
What do the variables actually contain? Are they variables you are going to put in a database table?
Thank you, but as I’ve shown in my examples, I can’t make it work
They contain rows extracted from a database, containing information to show to the user. I’m not using them to store anything in a database.
If it’s just the rows from a results set then your better off using a while loop:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$result_set[] = $row;
}
Immerse
September 27, 2011, 7:01pm
6
<?php
$first_bit_of_variable_name = 'woop';
for($i=0; $i<5; $i++) {
$variable_variable_name = $first_bit_of_variable_name.$i;
$$variable_variable_name = 'foobar'.$i;
}
var_dump($woop3); // => foobar3
?>
I couldn’t get it to work without the extra step of concatenating the $variable_variable_name first.
And also, more importantly, it’s totally unreadable if you look at this code a month later
Using an array is considerably more flexible. If you use variables, you’ll have no easy way of checking the number of variables. Looping through the variables or passing them on to functions will also be quite cumbersome. With an array, this is very easy to do.
salathe
September 27, 2011, 7:37pm
8
Immerse, your example could use
${$first_bit_of_variable . $i} = 'foobar'.$i;
The code within curly braces should evaluate to a string, which is the name of the variable. It needn’t just be variables either; function calls and other operations work too, so long as it’s a single statement (no semicolons).
Cups
September 27, 2011, 7:53pm
9
Say your result set from your database is retrieves logical field names like name, address.
These are available to you as say, $row[‘name’] $row[‘address’], agreed?
If you want to use them as named variables then you could just use the object notation instead of array notation eg
$row->name, $row->address
This is
a) easier to read
b) easier to type
c) can be used inside double quotes
system
September 28, 2011, 2:38am
10
As has been said by everyone here, just use an array. You’re VASTLY overthinking something simple AND trying to abuse ‘variable variables’ for something they really were never meant to do.
C_Ankerstjerne:
Using an array is considerably more flexible. If you use variables, you’ll have no easy way of checking the number of variables. Looping through the variables or passing them on to functions will also be quite cumbersome. With an array, this is very easy to do.
Yeah, I was thinking about using a for loop because I know the number of variables that I need.
Ok, thank you very much everybody!
Cups
September 28, 2011, 8:45am
12
a) means you will find errors far more easily
b) means your are less likely to make quoting errors
and I should have mentioned:
d) it introduces the idea of objects into your psyche
foreach
is faster than for
in looking through an array. And its syntax is prettier
Sorry for the late reply
Is it really faster? Why? _ I don’t know why, but I thought it was the opposite, because in a for loop I explicitly say what the upper limit is…
I just checked, and for
is indeed faster (and while
is faster still). Turns out the speed test I made gave foreach
a significant advantage.