Jump to content

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
https://forums.phpfreaks.com/topic/205019-variable-value-as-variable-name/
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.

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?

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

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.