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. Link to comment https://forums.phpfreaks.com/topic/241347-giving-two-variables-the-same-value/ 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. Link to comment https://forums.phpfreaks.com/topic/241347-giving-two-variables-the-same-value/#findComment-1239714 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? Link to comment https://forums.phpfreaks.com/topic/241347-giving-two-variables-the-same-value/#findComment-1239717 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; Link to comment https://forums.phpfreaks.com/topic/241347-giving-two-variables-the-same-value/#findComment-1239718 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. Link to comment https://forums.phpfreaks.com/topic/241347-giving-two-variables-the-same-value/#findComment-1239719 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. Link to comment https://forums.phpfreaks.com/topic/241347-giving-two-variables-the-same-value/#findComment-1239723 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.