Jump to content

[php] how to make the name of the variable be the same as another variable?


michalchojno

Recommended Posts

How to make the name of the variable be a string of text, which you have in another variable?

 

Example:

$var = 'variable';

$variable = 'This is just some text';

 

Now I don't want to name it just $variable, but the name comes as a result of $var. How do I do that?

 

I tried this, which didn't work:

$var = 'variable';

('$'.$var) = 'This is just some text'; // doesn't work

 

Any tips?

in the real world, if your variable data (and variable name) are changing, you'll echo it this way:

<?php
$var = "variable";
$$var = "some text";
echo $$var;

I don't know of a time where you will dynamically name a variable and then call a statically named variable.

OK, your code works.

 

Now why this code doesn't?

$word = ('$'."$items5[$counter]".'5[$pid]');

$var5[$counter] = $$word;

echo "$var5[$counter]";

 

The result is empty.

 

EDIT: Perhaps the $ sign in variable $word? But I changed that and still no result.

 

In other words that would go:

 

$var = "variable[$counter]";

$$var = "some text";

echo $$var;

 

Would that work?

 

using this script:

$counter = 1;
$items5[1] = "test";
$word = ('$'."$items5[$counter]".'5[$pid]');
print $word;

I got this echoed out:

$test5[$pid]

 

That's right. Me too. But now try:

$whatever = $$word

 

Will that work?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.