Alex C Posted May 26, 2010 Share Posted May 26, 2010 Hi, Sorry if this seems simple but I am going a bit mad. I have done this a thousand of times before and for some reason today it is not working. I just want to join two variables together like $item_number$i But for some reason it just wont work it only ever prints out the value of $i I swear the following used to work in PHP 4 I am using php 5 $item_number1 = "foo"; $item_number2 = "bar"; $i = 1; while($i <= 2) { echo "$item_number$i <br />"; $i++; } The above code just prints out: 1 2 Please can someone help, I am going insane Thanks Quote Link to comment https://forums.phpfreaks.com/topic/202986-help-create-a-dynamic-variable-by-joining-two-together-php-5/ Share on other sites More sharing options...
premiso Posted May 26, 2010 Share Posted May 26, 2010 I would suggest using an associative indexed array instead, but here is how you would achieve that: $item_number1 = "foo"; $item_number2 = "bar"; $i = 1; while($i <= 2) { echo ${$item_number . $i} . "<br />"; $i++; } Should do what you want. An array would be better a quicker, however. As an FYI. Quote Link to comment https://forums.phpfreaks.com/topic/202986-help-create-a-dynamic-variable-by-joining-two-together-php-5/#findComment-1063683 Share on other sites More sharing options...
Alex C Posted May 26, 2010 Author Share Posted May 26, 2010 Hi premiso Thanks for the quick reply, for some reason the following code doesn't print out anything $item_number1 = "foo"; $item_number2 = "bar"; $i = 1; while($i <= 2) { echo ${$item_number . $i} . "<br />"; $i++; } Have you any ideas? Can I ask is this a change in PHP 5 that has stopped the usual "$var$i" from working as its been bugging me all day. Quote Link to comment https://forums.phpfreaks.com/topic/202986-help-create-a-dynamic-variable-by-joining-two-together-php-5/#findComment-1063695 Share on other sites More sharing options...
kenrbnsn Posted May 26, 2010 Share Posted May 26, 2010 Try: <?php $item_number1 = "foo"; $item_number2 = "bar"; $i = 1; while($i <= 2) { echo ${'item_number' . $i} . "<br />"; $i++; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/202986-help-create-a-dynamic-variable-by-joining-two-together-php-5/#findComment-1063700 Share on other sites More sharing options...
Alex C Posted May 26, 2010 Author Share Posted May 26, 2010 Thanks kenrbnsn that works perfectly. Out of interest I have decided to use the array method as previously mentioned, this is a faster better technique? $item_number[1] = "foo"; $item_number[2] = "bar"; $i = 1; while($i <= 2) { echo $item_number[$i] . "<br />"; $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/202986-help-create-a-dynamic-variable-by-joining-two-together-php-5/#findComment-1063702 Share on other sites More sharing options...
kenrbnsn Posted May 26, 2010 Share Posted May 26, 2010 If you're going to use an array, I would suggest using either a for loop <?php $item_number = array(1=>'foo',2=>'bar'); for ($i=1;$i<3;++$i) { echo $item_number[$i] . "<br />\n"; }?> or a foreach loop <?php $item_number = array(1=>'foo',2=>'bar'); foreach ($item_number as $val) { echo $val . "<br />\n"; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/202986-help-create-a-dynamic-variable-by-joining-two-together-php-5/#findComment-1063704 Share on other sites More sharing options...
PFMaBiSmAd Posted May 26, 2010 Share Posted May 26, 2010 I have seen benchmarks that indicate that using an array is 3x faster than using variable variables. Quote Link to comment https://forums.phpfreaks.com/topic/202986-help-create-a-dynamic-variable-by-joining-two-together-php-5/#findComment-1063707 Share on other sites More sharing options...
Alex C Posted May 26, 2010 Author Share Posted May 26, 2010 Thanks for the great feedback guys, I always think its good to try and code to the best standards. I will implement this technique straight away. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/202986-help-create-a-dynamic-variable-by-joining-two-together-php-5/#findComment-1063716 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.