us2.php.net/manual/en/language.variables.variable.php
Th at is, a variable name which can be set and used dynamically. In the above example, hello, can be used as the name of a variable by using two dollar signs. In order to use variable variables with arrays, you have to resolve an am biguity problem.
You have to be very careful when dealing with use r input, especially GET and POST; because having variables automatically assigned to the names of those values is dangerous, and can lead to som eone manipulating the values of _other_ variables within the script, if they are of the same name. In any case, you could shorten those loops quite a bit by using foreach ( although I would take heed of the above warning; It 's recommended that you leave it set as false, though, as I said, in all newer PHP versions. You're advised to just stick with the 'superglobals ' -- if you just wanted to shorten the typing and be (very) lazy when it comes to not having to repeat $_GET or $_POST, lol, you could just assi gn, for example, $_G and $_P as aliases to the arrays (or references, if you want to save on the extra memory space that would be allotted if th ey were duplicates, ie $_G =& $_GET).
Say that you have a form for entering information about persons, but don't know in advance for how many persons your users want to enter informatio n Just provide an extra submit-button to add an extra entry in the form . The previously entered values from the form will be displayed again an d an extra form entry will be provided. There is no limit to how many ti mes the users can add entries. When finally hitting the submit button, a ll entries can be handled. php if ($submit=='submit'){ echo "Handle received data from form here";
It seems that the following function would attempt to overcome that issue for later versions of php. I haven't tried it out yet, as I have no need for it right now -- and it may pose some of the same issues as if you h ad register_globals set to "on" -- but here it is, anyway. function getRequestVariables() { foreach ($_POST as $postKey => $postValue) { global $$postKey;
i wanted to create the nam e of the variable on the fly and then get it's value. In other words, I wanted to dynamically build the name of a variable, and then evaluate it . let's say there are 3 million of these variables in a series... Now i want to find out which var has a value of "dog" - so i need to dyna mically build the name of the variable. You can do the FOR loop thing to create a string that represents the name of the variable, but how on ea rth do you evaluate a string as if it were a variable?
For instance, if you wanted to dynamically generate this series of variab les: base1_suffix1 base1_suffix2 base2_suffix1 base2_suffix2 base3_suffix1 base3_suffix2 You can do this: $bases = array('base1', 'base2', 'base3');
This is another example from the same program I use d in the previous entries. Essentially, I wanted to make a dynamically generated, multi-dimensi onal, associative array. The dynamic generation consists of an associat ion within the array of a certain key attribute within those variably-na med objects from my previous example. Yes, there are other, easier ways to associate objects than this man ner; however, for this particular project, the snippet is a solution for a requested "feature" on a website. That feature consists of displayin g the values from only certain, user-specified objects by their "key" va lue. note: the name of the variables/arrays change from example to example to avoid ambiguity within the script. Also, there are two different methods to place the $obj object into the array, and this was a trial to see which one worked. foreach ($this->obj_array as $key=>$obj){ // if the key already exists.. if ( array_key_exists($obj->GetKeyCode(), $this->new_array) ){ $key = $obj->GetKeyCode();
to continue my previous example of placing dynamically named objects into an array, we can easily pull everything back out again using a basi c array function: foreach.
Due to the rather odd structure of an input string I am currently parsing , I must have a reference for each particular object instantiation in th e order which they were created. In addition, because of the syntax of the input string, elements of the previous object creation are required for the current one. foreach ($input_array as $key=>$value){ // test to ensure the $value is what we need. I haven't fully tested the implimentation of the objects. Regardless, this is another example of the manner in which the var-vars can be used with precision where tedious, extra hard-coding is the only alternative.
This is probably the d esired behavior for constants, but was confusing for me when I was tryin g to figure it out. The alternative I used was to add the variables I n eeded to the $GLOBALS array instead of defining them as constants.
|