Articles
Ce document est aussi disponible en français
Variable variables are some of the tools that make PHP so dynamic. The name of a variable, as a string, may be used to access the actual value of this variable. One just have to multiply dollars.
<?php $a = 'b'; $b = 'c'; $c = 'd'; echo $$$a; // displays d ?>
This tool is particularly convenient for libraries which have to juggle with variables that will be defined by the programmer. The programmer will give the library a list of variables names, and the library will be available to reach them, just like pointer's pointer.
And, when those variables become too many, it is easier to gather them in an array, and process them with a loop.
<?php $variables = array('a' => 'A', 'b' => 'B', 'c' => 'C'); extract($variables); ?>
extract() read the array and create variables on the fly. It will take the key as the variable name, and the associated value as the actual value for the variable. This will wor le tableau pour créer des variables. Elle utilise la clé comme nom de variable finale, et la valeur associée comme valeur de la variable. L'exemple ci-dessus revient exactement à ceci :
<?php $variables = array('a' => 'A', 'b' => 'B', 'c' => 'C'); foreach($variables as $name => $value) { $$name = $value; } ?>
The argument of extract() must be indexed with strings. If the index cannot be used as a variable, it will simply be ignored.
The way the variables are generated may be adapted to the existings set of variables. The second arguments adapts the behavior of extract(), and may add a prefix (forced or upon existence), or simply forbid the erasure of existing variables. It is also possible to create references on the values, et not new values.
<?php $variables = array('_GET' => 'A'); extract($variables, EXTR_IF_EXISTS, 'extract'); // this extraction will not replace _GET if it already exists. ?>
The reverse of extract() is the compact() function. it will take the name of the variables as strings, and return an array with variable name as key, and associated value as value.
<?php $a = 'b'; $b = 'c'; $c = 'd'; print_r(compact(array('a',$b, 'd'))); /* Array ( [a] => b [c] => d ) // $d never existed */ ?>
compact() need the name of the variable and not the variable itself. Here, it is the $c variable that was used, and not $b. After a few mistake, you'll probably learn about this trick on your own.
extract() and compact() are most useful to handle situations where the number of arguments of a function is really big, or is only known at execution time. It will also be useful to handle lots of optionnal arguments.
<?php /// main code creates $b and $c $args = compact(array_flip(range('a','d')); $return = fonction($args); extract($return); // this will replace the valeurs in the main code function fonction($args) { extract($args); // variables modification return compact(array_flip(range('a','d')); } ?>
Keep in memory
- extracted values in the main code will be global. extracted values in a function will be local.
- Don't use extract() with $_GET and sisters, as this will emulate register_globals
- Use extract() on array that you know are safe : if those arrays are tweaked from outside the script, then all variables in your script may be corrupted.
| < Précédent | Suivant > |
|---|
Commentaires
Vous pouvez ajouter votre commentaire! |
Vous devez vous connecter pour commenter


