nac20 Posted August 20, 2008 Share Posted August 20, 2008 You should get the idea of what I'm trying to do from this: extract($_POST); // the above extract could result in the variables $hid_firstname_0 / $hid_lastname_0 as a minimum up to $hid_firstname_5 / $hid_lastname_5 depending on the number selected by the user (1-6), this value will be in $numLetters for ($i = 0; $i < 6; $i++) { $hid_firstname = '&$hid_firstname_'.$i; $hid_lastname = '&$hid_lastname_'.$i; $sql = "INSERT INTO tbl_letter(lt_firstname, lt_lastname) VALUES ('$hid_firstname', '$hid_lastname')"; } // The above code is actually putting the string '&$hid_firstname' into the table field rather than the value of this posted variable Someone suggested the following code but unfortunately this didn't work: $hid_firstname = "&$hid_firstname_$i"; $hid_lastname = "&$hid_lastname_$i"; Any further ideas would be greatly appreciated? Link to comment https://forums.phpfreaks.com/topic/120497-reference-variables/ Share on other sites More sharing options...
Fadion Posted August 20, 2008 Share Posted August 20, 2008 The code syntax isn't right. Try: <?php for($i = 0; $i < 6; $i++){ $hid_firstname = &${"hid_firstname_$i"}; $hid_lastname = &${"hid_lastname_$i"}; } ?> Is there any reason you are referencing the variables. $hid_firstname will get the content of the post values, while the post variables retain those values, so there's no point for referencing as it will do nothing. Link to comment https://forums.phpfreaks.com/topic/120497-reference-variables/#findComment-620946 Share on other sites More sharing options...
nac20 Posted August 20, 2008 Author Share Posted August 20, 2008 I'm a complete newbie to php so GulityGear please can you clarify why I don't need to use reference variables. I don't know how many instances of $hid_firstname_? there will be hence my thought to use references. I know extracting the post will give me the contents but I still need a mechanism for looping through these. An example of what you mean would really help. Thanks. Link to comment https://forums.phpfreaks.com/topic/120497-reference-variables/#findComment-620969 Share on other sites More sharing options...
Fadion Posted August 20, 2008 Share Posted August 20, 2008 The reference variable becomes an alias of another variable, which contains the same content. Basically: <?php $foo = 'foo'; $bar = &$foo; echo $bar; //will print 'foo' $bar = 'bar'; echo $foo; //wil; print 'bar', instead of 'foo' ?> The $bar and $foo are referenced and share the same content. In your case though, you don't have to reference to a post variable, as you don't need it. Just assigning the post variable to another variable will do the work. As for the rest of code it is ok. The for() will loop through all the extracted variables and insert the info into the db. Guess you won't have problems with that. Modify your code with the snippet I suggested and remove the reference (&). Link to comment https://forums.phpfreaks.com/topic/120497-reference-variables/#findComment-620975 Share on other sites More sharing options...
PFMaBiSmAd Posted August 20, 2008 Share Posted August 20, 2008 Variable variables are 3x slower than using an array. Any time you have a set (two or more) of same type values you should use an array. The code will be simpler (you can use a foreach loop on an array) and the code will be faster. Link to comment https://forums.phpfreaks.com/topic/120497-reference-variables/#findComment-621066 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.