Jump to content

Giving two variables the same value


c_pattle

Recommended Posts

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

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.

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?

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. 

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.