...

View Full Version : combining variables



mtd
06-23-2005, 02:30 PM
Is there a way to define one variable's value as the names (not values) of two other variables?

i.e.
$var1 = 'ONE';
$var2 = 'TWO';

New variable ($var3) needs to be named '$var1var2'. A simple combo like:

$var3 = $var1.$var2;

works with the values of $var1 and $var2, yielding "ONETWO". So that doesn't work.

Maybe this is really easy? really hard? against the nature of PHP? I figured there's got to be a way!

mattyod
06-23-2005, 03:06 PM
Hmm maybe I'm missing the point of your question but simply.

$var3 = '$var1'.'$var2';

will return the value you want i.e $var1$var2 but you are just passing in a string not doing anything dynamic. So this would work too:

$var3 = '$var1$var2';

Maybe I'm missing the point but I don't see why you are giving variables to $var1 & $var2 in order to declare another variable based on thier names. By which I mean that


<?php
$var1 = 'ONE';
$var2 = 'TWO';
$var3 = '$var1$var2';

echo $var3;
?>

will return the same as:


<?php
$var3 = '$var1$var2';

echo $var3;
?>

mrruben5
06-23-2005, 03:49 PM
Shouldn't the dollar signs be escaped?
$var1 = 'ONE';
$var2 = 'TWO';
$var3 = '\$var1\$var2';

delinear
06-23-2005, 03:52 PM
Only with double quotes - single quotes as used will treat the $ purely as a part of the string, it shouldn't try to parse.

mrruben5
06-23-2005, 03:53 PM
So that's why my \n line breaks don't work with single quotes huh :)

delinear
06-23-2005, 03:55 PM
Yep :D I had that problem a lot when I first started out, wondering why I had \n's all over everything. The answer is always a lot more simple than you'd expect :p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum