PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Variablen automatisch generieren


frakor
29.04.2002, 13:09:28
Hallo!

Ich würde gerne Variablennamen wie $position1, $position2, usw.
automatisch generieren.

Sind Teil eines Formulars
echo "<input type=text name='position{$num}' size='5' maxlength=10 value='$irgendwas'>";

Funktioniert im Formular (Browser), dort kann ich auch mit JS problemlos auf alle einzelnen zugreifen usw.
Nach dem Submit mit Methode POST hab ich allerdings Probleme aus PHP darauf zuzugreifen.

Wenn ich testweise händisch
echo" $position2 ";
angebe, erhalte ich problemlos den aktuellen Inhalt der Variablen.

Nur wie erstelle ich jetzt automatisch die fortlaufenden Variablennamen?

Bitte um Hilfe und vielen Dank!

Franz

Progman
29.04.2002, 13:47:18
$fester_teil="postition";
for($i=0;$i<1337;$i++)
(
$tmp=$fester_teil.$i;
$Var=$$tmp;
echo($tmp." hat den Wert ".$Var."
<br>");
//oder
echo($tmp." hat den Wert ".$$tmp."
<br>");
}

frakor
29.04.2002, 13:58:28
Vielen Dank!

Ist ja ein klassischer Zeiger...
(aus dem PHP Manual http://www.php.net)

Variable variables
Sometimes it is convenient to be able to have variable variable names. That is, a variable name which can be set and used dynamically. A normal variable is set with a statement such as:

$a = "hello";
A variable variable takes the value of a variable and treats that as the name of a variable. In the above example, hello, can be used as the name of a variable by using two dollar signs. i.e.

$$a = "world";
At this point two variables have been defined and stored in the PHP symbol tree: $a with contents "hello" and $hello with contents "world". Therefore, this statement:

echo "$a ${$a}";
produces the exact same output as:

echo "$a $hello";
i.e. they both produce: hello world.

In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.

LG Franz