View Full Version : I can do this in Flash, it it possible in PHP?
kozel
04-27-2006, 05:48 PM
Hi,
in Flash I can do this:
var1 = "something";
var2 = "something else";
var3 = "something different";
for (i=0;i<5;i++)
{
trace (String(var + i)); //traces: something// something else// something different
};
____________________________________
I'd like to do the same in PHP:
$var1 = "something";
$var2 = "something else";
$var3 = "something different";
while($i<5) {
echo ...???...//And here is the problem. I cannot use $var.$i, right?
i++;
};
Can you help me, pls??
Flash Gordon
04-27-2006, 05:59 PM
$i=0
while() {
$i++;
}
works just fine ;)
EDIT: Oh i see, your not just talking about not just syntax errors but ....yea i see. Let me think about this.
Well here is a start, at least I fixed the syntax errors:
<?php
$var = "null";
$var1 = "Hello";
$var2 = "World";
$var3 = "Nee";
$var4 = "foo";
$i = 1;
while ($i<5) {
echo $var.$i . " | ";
$i++;
}
?>
jsebrech
04-28-2006, 08:27 AM
You can do this with the PHP eval (http://www.php.net/manual/en/function.eval.php) function.
kozel
04-28-2006, 02:17 PM
Thanks, I still cannot get it.
It's not very clear to me how to use the eval function in PHP.
Actually I need a growing string. Like this:
$i = 1;
$myString = "";
$var1 = "one";
$var2 = "two";
$var3 = "three";
...
$var100 = "hundered";
for ($i<100) {
$myString .= $var.$i; //OF COURSE THIS WOULN'T WORK, JUST FOR THE IDEA.
}
how to make the loop go through all my var1,var2...var100?
Thanks
jsebrech
04-28-2006, 02:35 PM
$myString .= eval('return $var' . $i . ';');
Basically the argument to eval is the string for the contents of a function that does what you want, with the exception that the variables inside of your eval'd code have the same scope as those of the code you called eval from.
invader
05-02-2006, 10:32 PM
use this:
${"var".$i}
variable variable names (yes, that's variable names that are variable o.O) are very powerful in php. it will evaluate what's inside the curly braces, and try to access the variable with that name
edit: php.net is an excellent resource.. you should definitely practice doing searches like this:
site:php.net "variable variable names"
(second result for that search is this page: http://us2.php.net/language.variables.variable )
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.