c_pattle Posted July 7, 2011 Share Posted July 7, 2011 I'm trying to give two variables the same value which I know you can do with the following $variable1 = $variable2 = "value"; However I want to concatenate this value to two variables. However if I use the code below it also concatenates the value in $variable2 to $variable1. $variable1 .= $variable2 .= "value"; Is there a way to make the above work? I'm just thinking it will save me some lines of code. Quote Link to comment Share on other sites More sharing options...
cssfreakie Posted July 7, 2011 Share Posted July 7, 2011 you might want to try the following: <?php $var1= 3; $var2 = &$var1; // here you link them $var2 = 5; echo $var1; // will out put 5 ?> $var1 and $var2 are now linked to each other. changing the value of one will change the value of the other. Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted July 7, 2011 Share Posted July 7, 2011 cssfreakie's code will create a refernce to the first var..so if you change the value of one variable the other variable's value will change as well. However I'm not sure if this is what you are trying to accomplish? What exactly is your goal here for these two variables by using concatenation? Quote Link to comment Share on other sites More sharing options...
xyph Posted July 7, 2011 Share Posted July 7, 2011 Or just write it like this $var1 .= $append; $var2 .= $append; Quote Link to comment Share on other sites More sharing options...
c_pattle Posted July 7, 2011 Author Share Posted July 7, 2011 What I want to achieve is that I have to text string and I just want to append the word "and" to them both at the same point in my program. However I don't want them to be linked because they will be different string. I just thought it might be more efficient and it will save on code if I could append "and" to them both at the same time. Quote Link to comment Share on other sites More sharing options...
xyph Posted July 7, 2011 Share Posted July 7, 2011 If you're doing it many, many times, make a function. Otherwise my code is the shortest way. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.