Toon posts:

[PHP] variabele variabele

Pagina: 1
Acties:
  • 212 views sinds 30-01-2008

Verwijderd

Topicstarter
Ik wil een variabele variabele in php dus bijvoorbeeld:

PHP:
1
2
3
4
5
6
for($i=0;$i<$totaal;$i++)
    {
        echo $checkbox.$i;
        echo " checkbox".$i."<BR>";
        
    }


het moet kunnen ik ben er alleen nog niet helemaal uit hoe...

kon ook niks vinden in de search

  • marcusk
  • Registratie: Februari 2001
  • Laatst online: 26-09-2023
al gekeken in de php manual ? en als je "checkbox[]" als naam instelt kun je ze als array gebruiken.

  • Apollo_Futurae
  • Registratie: November 2000
  • Niet online
in plaats van variabele variabelen moet je hier arrays gebruiken

Pas de replâtrage, la structure est pourrie.


Verwijderd

Topicstarter
ik heb het boek PHP4 het complete handboek van AC (al een keer helemaal doorgelezen) en ik heb gezocht, met google en met GoT en ook op php.net

ik zoek dus een antwoord of een link waar ik het kan lezen, trefwoorden, want ik kan niks vinden op "php variabele variabele"...

Verwijderd

Topicstarter
Het moet kunnen, variabele variablen, een vrien van me heeft het ook een keer gebruikt, maar ik kan hem op het moment even niks vragen...

  • gorgi_19
  • Registratie: Mei 2002
  • Laatst online: 29-08 12:08

gorgi_19

Kruimeltjes zijn weer op :9

Verwijderd schreef op 29 oktober 2002 @ 21:18:
[...]


ik heb het boek PHP4 het complete handboek van AC (al een keer helemaal doorgelezen) en ik heb gezocht, met google en met GoT en ook op php.net

ik zoek dus een antwoord of een link waar ik het kan lezen, trefwoorden, want ik kan niks vinden op "php variabele variabele"...
Kijk anders eens in PW, kies onderaan voor ".. laatste 31 dagen" (of langer) en gebruik Control + F eens met het zoekwoord: variabele
Ik kan me herinneren dat ik dit onderwerp afgelopen tijd toch al meerdere keren voorbij heb zien komen...

Digitaal onderwijsmateriaal, leermateriaal voor hbo


  • marcusk
  • Registratie: Februari 2001
  • Laatst online: 26-09-2023
Verwijderd schreef op 29 oktober 2002 @ 21:18:
ik zoek dus een antwoord of een link waar ik het kan lezen, trefwoorden, want ik kan niks vinden op "php variabele variabele"...
kijk nog eens goed naar mijn post (en die van Apollo_Futurae), daar staat dus een link.

Verwijderd

tuurlijk kan het
het is vies
het kan altijd anders
maar het kan

In de manual staan voorbeelden:
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.

Please note that variable variables cannot be used with PHP's new superglobals. This means you cannot do things like ${$_GET}. If you are looking for a way to handle availability of superglobals and the old HTTP_*_VARS, you might want to try referencing them.

  • pjonk
  • Registratie: November 2000
  • Laatst online: 29-12-2025
Om toch effe een beetje offtopic te reageren.
Ik vind het gebruik van variabele variabelen behoorlijk dirty. Er is altijd wel een beter en cleaner alternatief te gebruiken zoals in dit geval.

It’s nice to be important but it’s more important to be nice


Verwijderd

Topicstarter
die had ik net even gemist ik had mijn reply al getypt, sorry, ben nu aan het lezen

Verwijderd

${$var}

  • drm
  • Registratie: Februari 2001
  • Laatst online: 09-06-2025

drm

f0pc0dert

manual werk

tipje: gebruik liever arrays

Music is the pleasure the human mind experiences from counting without being aware that it is counting
~ Gottfried Leibniz

Pagina: 1

Dit topic is gesloten.