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
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.

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.