kworden Posted December 4, 2006 Share Posted December 4, 2006 What I'm trying to do is display a list of Values with A # value added to after the original value it represents...See an example below:[code]$ga1 = 4;$ga2 = 3;$ga3 = 1;$ga4 = 2;$ga5 = 4;$ga6 = 2; {Edited to add ';' I forgot to add when posting}$numberofgaquestions = 34;for($i=1; $i<= $numberofgaquestions; $i++ ){ echo "The # $i: ". $ga$i ."<br>";}[/code]The result I'm looking for is:The # 1: 4The # 2: 3The # 3: 1The # 4: 2The # 5: 4The # 6: 2I get an error saying "Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' " So how can i add the # value to another variable?Thanks for your help in advance.... K:) Link to comment https://forums.phpfreaks.com/topic/29451-adding-a-value-to-the-end-of-another-variable/ Share on other sites More sharing options...
papaface Posted December 4, 2006 Share Posted December 4, 2006 You need to put ;'s at the end of your variables.[code]$ga1 = 4;$ga2 = 3;$ga3 = 1;$ga4 = 2;$ga5 = 4;$ga6 = 2;$numberofgaquestions = 34;for($i=1; $i<= $numberofgaquestions; $i++ ){ echo "The # $i: ". $ga$i ."<br>";}[/code] Link to comment https://forums.phpfreaks.com/topic/29451-adding-a-value-to-the-end-of-another-variable/#findComment-135134 Share on other sites More sharing options...
mainewoods Posted December 5, 2006 Share Posted December 5, 2006 you can use the $$ variable variable operator:[code]for($i=1; $i<= $numberofgaquestions; $i++ ){ $varname = 'ga' . $i; //create name of variable as a string echo "The # $i: ". $$varname ."<br>"; // notice $$}[/code]--that will work but is not recommended, rewrite using array variables Link to comment https://forums.phpfreaks.com/topic/29451-adding-a-value-to-the-end-of-another-variable/#findComment-135272 Share on other sites More sharing options...
kworden Posted December 5, 2006 Author Share Posted December 5, 2006 mainewoods [b]Thank You![/b] that worked for me. I'm only using it to double check data from a database...Thanks....K :) Link to comment https://forums.phpfreaks.com/topic/29451-adding-a-value-to-the-end-of-another-variable/#findComment-135274 Share on other sites More sharing options...
JasonLewis Posted December 5, 2006 Share Posted December 5, 2006 but take not of what mainewoods said. It is not good practice, defiantly use ARRAYS for things like this. Link to comment https://forums.phpfreaks.com/topic/29451-adding-a-value-to-the-end-of-another-variable/#findComment-135340 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.