play_ Posted August 6, 2007 Share Posted August 6, 2007 Hi again. I have a for() loop, where $i increments to 10. Inside the loop, i wanna create a variable(say $var_);, with $i appended at the end. So everytime the loop goes around, i'd get $var_1, $var_2, $var_3.........$var_10. doing $var_$i = 'string' doesn't work. Link to comment https://forums.phpfreaks.com/topic/63474-solved-quick-help-regarding-variable-name/ Share on other sites More sharing options...
teng84 Posted August 6, 2007 Share Posted August 6, 2007 i dont get it but are you referring with concatenation then use the dot . after another variable Link to comment https://forums.phpfreaks.com/topic/63474-solved-quick-help-regarding-variable-name/#findComment-316325 Share on other sites More sharing options...
redarrow Posted August 6, 2007 Share Posted August 6, 2007 $var[$i] Link to comment https://forums.phpfreaks.com/topic/63474-solved-quick-help-regarding-variable-name/#findComment-316328 Share on other sites More sharing options...
Fadion Posted August 6, 2007 Share Posted August 6, 2007 dont know what u need that for but there are arrays for that m8 $var = array(); for($i=1; $i<=10; $i++){ $var[$i] = 'string'; //it will asign to $var[1] - $var[10] the value 'string' } Link to comment https://forums.phpfreaks.com/topic/63474-solved-quick-help-regarding-variable-name/#findComment-316330 Share on other sites More sharing options...
play_ Posted August 6, 2007 Author Share Posted August 6, 2007 I am concatenating, but not to output. I wanna concatenate to create a variable here's my code for($i = 0; $i <= 10; $i++) { // i wanna do something like this. So it would create 10 variables. $var_0, $var_1, $var_2....etc $var_ $i = "some string"; } } ?> Link to comment https://forums.phpfreaks.com/topic/63474-solved-quick-help-regarding-variable-name/#findComment-316331 Share on other sites More sharing options...
play_ Posted August 6, 2007 Author Share Posted August 6, 2007 The problem is it can't be an array. Guess i should've mentioned that Link to comment https://forums.phpfreaks.com/topic/63474-solved-quick-help-regarding-variable-name/#findComment-316332 Share on other sites More sharing options...
tippy_102 Posted August 6, 2007 Share Posted August 6, 2007 new_variable = "var_" . '$i'; ??? Link to comment https://forums.phpfreaks.com/topic/63474-solved-quick-help-regarding-variable-name/#findComment-316334 Share on other sites More sharing options...
BlueSkyIS Posted August 6, 2007 Share Posted August 6, 2007 and then you can refer to it as a variable: ${"var_" . $i} Link to comment https://forums.phpfreaks.com/topic/63474-solved-quick-help-regarding-variable-name/#findComment-316336 Share on other sites More sharing options...
play_ Posted August 6, 2007 Author Share Posted August 6, 2007 Thanks tippy, but that wouldn't work =) Link to comment https://forums.phpfreaks.com/topic/63474-solved-quick-help-regarding-variable-name/#findComment-316338 Share on other sites More sharing options...
wildteen88 Posted August 6, 2007 Share Posted August 6, 2007 Use: $i = 10; $var_10 = 'hello'; echo ${'var_' . $i}; EDIT Beaten to it by tippy, But anyways if you want to use variables value as part of a variables name you'll have to use variable variables, which what the above is Link to comment https://forums.phpfreaks.com/topic/63474-solved-quick-help-regarding-variable-name/#findComment-316339 Share on other sites More sharing options...
Barand Posted August 6, 2007 Share Posted August 6, 2007 <?php for ($i=1 ; $i <= 10 ; $i++) { $varname = "var_$i"; $$varname = $i; } echo $var_5; // --> 5 ?> As said, I can't see why anyone would want to do this now the array has been invented. Link to comment https://forums.phpfreaks.com/topic/63474-solved-quick-help-regarding-variable-name/#findComment-316340 Share on other sites More sharing options...
play_ Posted August 6, 2007 Author Share Posted August 6, 2007 Couldn't i do ${'var_'.$i} = 'string' ? Link to comment https://forums.phpfreaks.com/topic/63474-solved-quick-help-regarding-variable-name/#findComment-316341 Share on other sites More sharing options...
play_ Posted August 6, 2007 Author Share Posted August 6, 2007 <?php for ($i=1 ; $i <= 10 ; $i++) { $varname = "var_$i"; $$varname = $i; } echo $var_5; // --> 5 ?> As said, I can't see why anyone would want to do this now the array has been invented. I already have a multidimensional array(3 arrays nesting). don't want more though. Link to comment https://forums.phpfreaks.com/topic/63474-solved-quick-help-regarding-variable-name/#findComment-316342 Share on other sites More sharing options...
play_ Posted August 6, 2007 Author Share Posted August 6, 2007 Yes that worked. <?php $form = array(); for($i = 0; $i <= 10; $i++) { if(!empty($_POST["option_$i"])) { ${'row_'.$i} = array(); $form[] = ${'row_'.$i}; print_r($form); } } ?> Thanks eeeeeveryone. Link to comment https://forums.phpfreaks.com/topic/63474-solved-quick-help-regarding-variable-name/#findComment-316343 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.