Appending a Value of a Variable to a Variable Name

Appending a value of a variable to a variable name?

You need variable variables:

echo ${'part_'.$v};
// or
$var = 'part_'.$v;
echo $$var;

Appending string to variable name

The right was is to use either an array or an object, depending on if the id is going to be sequential or named.

var card_links_grid = {};
var card_id = "the_hanged_man";
card_links_grid[card_id] = "some value";

or

var card_links_grid = [];
card_links_grid.push("some value");

Variable variables are possible, but only with some hard to maintain and debug approaches that should be avoided. Use object types designed for the data structure you want to represent.

How to appending variable to a variable name

$item is an Object so you have to call its attribute:

$inputval = $item->{'ans_cont'.$i};
// Or
$inputval = 'ans_cont'.$i;
echo $item->$inputval;

How do I append a variable to create a new variable name

if you really need that's variable (like as you want), you can use braces {} to create variable from joined strings.

while($row = mysql_fetch_array( $sql )) {
${'opc'.$num} = $row['opcRes'];
$num++;
}

so you can access your variables

for($i = 1; $i < $num; $i++){
echo "<input type=\"radio\" name=\"".${'opc'.$i}."\" value=\"".${'opc'.$i}."\">".${'opc'.$i}."<br/>";
}

Append a value dynamically to js variable name

I'm not quite certain that this is what you're asking, but if you want dynamically named variables, you would set it on an object.

var id = 1234;
var varHolder = {};
varHolder['min' + id] = 24;
console.log(varHolder['min1234']);

If you need to access it as a normal variable, you would set it on the window object. I would recommend against this approach; it just screams design flaw if this is the only way to achieve what you're after

var id = 1234;
window['min' + id] = 24;
console.log(min1234);

python append list incrementing variable name

You can try elevate the exec function.

index = [1,2,4,8,16,32,64]
index2 = [100, 1000, 10000, 100000, 1000000]
for i in index:
for j in index2:
exec("temp_variable = df_foo_{0}_{1}".format(i, j))
temp.append(temp_variable)

Appending a value to a global variable name calling it later

As suggested here Dynamically create variable in javascript and assign value

You can do this:

var globalVar = 5;function test(value) {window[value+''+globalVar] = 'newValue';}
test("myName");
console.log(myName5);

Append a number in the end of a variable name in java

You can't create members dynamically in Java (you can access them dynamically via reflection, but there's no need for it here).

Rather than having

public javax.swing.JPanel BenderPanel1;
public javax.swing.JPanel BenderPanel2;
public javax.swing.JPanel BenderPanel3;

have

public javax.swing.JPanel[] BenderPanels;

or

public List<javax.swing.JPanel> BenderPanels;

Then you can loop through them with an enhanced for loop.

for (javax.swing.JPanel panel : BenderPanels) {
// ...
}


Related Topics



Leave a reply



Submit