Jump to content

variable value as variable name


chiprivers

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.