Forum Moderators: coopster

Message Too Old, No Replies

variable variables

combining variables

         

WhosAWhata

6:01 pm on Jan 18, 2004 (gmt 0)

10+ Year Member



this is a simplified version of the code that is running through many pieces of my site

$c1 = "hello";
$a = 1;
$b = c$a;
echo "$$b";

the only way i can get this to work is if i first compile $$b with another function

$c1 = "hello";
$a = 1;
$b = c$a;
// $b = functionOfSomeSort($$b);
echo "$b";

what would be the best "functionOfSomeSort()" to use?

jamesa

6:18 pm on Jan 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure what your asking, but I know that this will work:

$c1 = "hello";
$a = 1;
$b = "c" . $a;
echo $$b;

Does that help at all? :)

ergophobe

8:03 pm on Jan 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Or in some situations

eval("\$b = c$a1;");

depending on what your "unsimplified" version is. Clearly, in your simplified example, the previous post offers a better solution.

Tom

WhosAWhata

1:05 am on Jan 19, 2004 (gmt 0)

10+ Year Member



works perfect thanks
if you don't mind, how exactly does it work? i don't seem to understand this line

$b = "c" . $a;

it works but i don't know how it works
thank you

coopster

1:19 am on Jan 19, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



  1. The letter "c" is being concatenated to the value of the variable $a (which is "1") and that value is being assigned to the variable $b. Therefore, $b becomes "c1".
  2. Then, by taking the variable variable [php.net] of $b ($$b is a variable variable), you get the value of $c1.
I once tried to explain variable variables a bit more clearly, this read may help:
[webmasterworld.com...]

jamesa

4:03 am on Jan 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yea, by putting "c" in quotes it makes the "c" a string. Otherwise PHP expects another variable or a function (or perhaps an integer). Actually come to think of it,
$b = "c$a";
would probably work too. As long as the quotes are there it treats it as a string.

WhosAWhata

4:25 am on Jan 19, 2004 (gmt 0)

10+ Year Member



thanks that makes sense

dcrombie

11:15 am on Jan 19, 2004 (gmt 0)



$c1 = "hello";
$a = 1;
echo ${"c$a"};

;)

ergophobe

4:03 pm on Jan 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




echo ${"c$a"};

I would not have thought of that one!

jatar_k

4:52 pm on Jan 19, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the same applies inside the braces

$c1 = "hello";
$a = 1;
echo ${"c" . $a};

ergophobe

8:28 pm on Jan 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




the same applies inside the braces

But as the previous poster pointed out, substitution within double quotes works.

$c1= "hello jatar_k";
$a = "1";
echo ${"c$a"};

This outputs "hello jatar_k" just as promised.

Tom

jatar_k

10:52 pm on Jan 19, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



true but sometimes one style is easier to read than others for any particular person.

both work exactly the same

echo ${"c$a"};
echo ${"c" . $a};

I always use the second because I find it much easier to read. I just mentioned it to add to the many forms for the same function. ;)

g1smd

12:34 am on Jan 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ah, this simplifies some code that I was working on too.

I had used a two dimensional array.

This variable variable stuff will actually be better in many ways, I think.

WhosAWhata

4:52 pm on Jan 20, 2004 (gmt 0)

10+ Year Member



thanks guys,
code works great
before this i was using
$a1 = "hi";
$b = 1;
$c = a$b;
$c = stripslashes(htmlspecialchars($$c));
echo $c;

as you can see,
echo ${"a$b"};
works ALOT better