chiprivers Posted June 17, 2010 Share Posted June 17, 2010 In a script I am working on, I would like to be able to use the value of one variable to form part of the name of a new variable. For example, where the value of $x is 'text' I would like to be able to create a variable called $new_text. I have googled this but cannot find anyone who has done exactly this. I can get it to work in part like this: <?php $x = 'text'; ${$x} = 'value of new variable'; ?> this above script results in a new variable called $text however if I try the following code to have the value of the initial variable as just part of the name of the new variable, it does not work: <?php $x = 'text'; $new_{$x} = 'value of new variable'; ?> I would have liked this to create the new variable called $new_text but I get a parse error. Does anybody know how I can get this to work? Ultimately, I will want to be able to use two or more variable values to create the name of one new variable, ie: <?php $x = 'var1'; $y = 'var2'; $z = 'var3'; ${$x}_{$y}_{$z} = 'new variable value'; ?> which I would then like to result in a new variable which is called $var1_var2_var3. Quote Link to comment https://forums.phpfreaks.com/topic/205019-variable-value-as-variable-name/ Share on other sites More sharing options...
Garethp Posted June 17, 2010 Share Posted June 17, 2010 $Var = "Something"; $$Var = "This is a test"; echo $Something; Quote Link to comment https://forums.phpfreaks.com/topic/205019-variable-value-as-variable-name/#findComment-1073290 Share on other sites More sharing options...
chiprivers Posted June 17, 2010 Author Share Posted June 17, 2010 $Var = "Something"; $$Var = "This is a test"; echo $Something; I have already got this much to work but I need the new variable name to contain the value of the original variable plus further text if that makes sense? Quote Link to comment https://forums.phpfreaks.com/topic/205019-variable-value-as-variable-name/#findComment-1073292 Share on other sites More sharing options...
Garethp Posted June 17, 2010 Share Posted June 17, 2010 <?php $Var = "Something"; $NewName = $Var . "T"; $$NewName = "This is a test"; echo $SomethingT; ?> Would this suffice? Quote Link to comment https://forums.phpfreaks.com/topic/205019-variable-value-as-variable-name/#findComment-1073294 Share on other sites More sharing options...
chiprivers Posted June 17, 2010 Author Share Posted June 17, 2010 Ah yes, I guess that would work! Create the name of the new variable as the value of a temporary variable and then use this to name the new variable. Many thanks, I will give that a go Quote Link to comment https://forums.phpfreaks.com/topic/205019-variable-value-as-variable-name/#findComment-1073296 Share on other sites More sharing options...
trq Posted June 17, 2010 Share Posted June 17, 2010 Heading down this path makes for hard to maintain code. Why exactly do you think this is required? Quote Link to comment https://forums.phpfreaks.com/topic/205019-variable-value-as-variable-name/#findComment-1073297 Share on other sites More sharing options...
Alex Posted June 17, 2010 Share Posted June 17, 2010 I think this is what you're looking for: $var = 'variable'; ${'new_' . $var} = 'value'; echo $new_variable; // value Quote Link to comment https://forums.phpfreaks.com/topic/205019-variable-value-as-variable-name/#findComment-1073298 Share on other sites More sharing options...
chiprivers Posted June 17, 2010 Author Share Posted June 17, 2010 Heading down this path makes for hard to maintain code. Why exactly do you think this is required? The project that I am working on is rather complicated to explain but at the moment this is the easiest way for me to get my head around naming the variables and being able to recall them logically. There will be a large number of variables used in calaculations which all ultimately end up beind displayed on the page in a grid format. Once I have got my head around the script and getting it all working, I may be able to find an easier structure for naming the variables but at the moment, at least, this I think is my best option. Quote Link to comment https://forums.phpfreaks.com/topic/205019-variable-value-as-variable-name/#findComment-1073300 Share on other sites More sharing options...
trq Posted June 17, 2010 Share Posted June 17, 2010 Variable variables are far less efficient than using arrays. And the bigger more complicated your program is the more this (efficiencies) and readability will matter. I urge you to rethink your logic. Quote Link to comment https://forums.phpfreaks.com/topic/205019-variable-value-as-variable-name/#findComment-1073301 Share on other sites More sharing options...
chiprivers Posted June 17, 2010 Author Share Posted June 17, 2010 Are you suggesting that instead of using: <?php $var1 = 'var1'; $var2 = 'var2'; $var3 = 'var3'; $temp = $var1 . '_' . $var2 . '_' . $var3; ${$temp} = 'new variable'; echo $var1_var2_var3; ?> I should use: <?php $var1 = 'var1'; $var2 = 'var2'; $var3 = 'var3'; ${$var1}[{$var2}][{$var3}] = 'new variable'; echo $var1['var2']['var3']; ?> This structure would be just as easy for me to use. Would the syntax I have used in this example work or is there a different way I would need to declare the values of the multi-dimensional array? Quote Link to comment https://forums.phpfreaks.com/topic/205019-variable-value-as-variable-name/#findComment-1073304 Share on other sites More sharing options...
trq Posted June 17, 2010 Share Posted June 17, 2010 Whatever you just posted is not valid, nor an array. More like.... <?php $store = array(); $var1 = 'var1'; $var2 = 'var2'; $var3 = 'var3'; $store[$var1][$var2][$var3] = 'new variable'; ?> I really think your over complicating for no reason / benefit. Quote Link to comment https://forums.phpfreaks.com/topic/205019-variable-value-as-variable-name/#findComment-1073305 Share on other sites More sharing options...
chiprivers Posted June 17, 2010 Author Share Posted June 17, 2010 Many thanks for your input - I think that array structure will be ideal for what I am trying to do so I will amend my approach to utilise this. Quote Link to comment https://forums.phpfreaks.com/topic/205019-variable-value-as-variable-name/#findComment-1073306 Share on other sites More sharing options...
trq Posted June 17, 2010 Share Posted June 17, 2010 Sweet, its a much better approach. Quote Link to comment https://forums.phpfreaks.com/topic/205019-variable-value-as-variable-name/#findComment-1073313 Share on other sites More sharing options...
PFMaBiSmAd Posted June 17, 2010 Share Posted June 17, 2010 You will find that when you have a set of data that you are trying to process, that using an array will result in the simplest and most efficient code (sets of data are what arrays are for.) Creating a series of named variables takes more code and then you must either keep track of how many you created and what they were named or hard-code the logic that references them (which defeats the point of using a programming language to simplify a repetitive task.) Quote Link to comment https://forums.phpfreaks.com/topic/205019-variable-value-as-variable-name/#findComment-1073397 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.