Page 1 of 1
Variables
Posted: Tue Feb 19, 2013 5:43 pm
by YoussefSiblini
Hi,
I have this code:
Code: Select all
<?php
$variable = "a value";
$store = $variable;
?>
What I want to achieve is to store the value of $variable in $store so the value of $store is now "a value" but if $variable changed to some thing different lets say "new value" I want $store to still "a value".
Is there a way to do this?
Jo
Re: Variables
Posted: Tue Feb 19, 2013 6:21 pm
by requinix
That's what it does already.
Code: Select all
$variable = "a value";
$store = $variable;
$variable = "a different value";
echo $store; // a value
Assignments work by making a copy of the value, not by "referencing" the other variable.
Re: Variables
Posted: Tue Feb 19, 2013 6:46 pm
by Christopher
But you can make $store a reference to $variable:
Code: Select all
$variable = "a value";
$store = &$variable;
echo "$store<br/>";
$variable = 'B VALUE';
echo "$store<br/>";
However, the fact that you are doing this is a strong indication that whatever you are doing is the wrong way. But the best way to learn that, is to try it this way first!
Re: Variables
Posted: Wed Feb 20, 2013 3:08 am
by YoussefSiblini
Hi Thanks for your reply,
The above code is outputting:
[text]
a value
B VALUE
[/text]
$store is still changing with $variable.
I know that there are a better ways to do this, I will get there slowly

Re: Variables
Posted: Wed Feb 20, 2013 3:49 am
by requinix
YoussefSiblini wrote:Hi Thanks for your reply,
The above code is outputting:
[text]
a value
B VALUE
[/text]
$store is still changing with $variable.
I know that there are a better ways to do this, I will get there slowly

If you don't read what we say then there's nothing we can do to help you.
I told you that the
normal behavior in PHP is exactly what you want. Christopher told you that the behavior you
don't want is still possible with a special syntax.
Re: Variables
Posted: Wed Feb 20, 2013 4:29 am
by YoussefSiblini
lol it is still morning here, sorry didn't notice this:
Code: Select all
Re: Variables
by requinix ยป Tue Feb 19, 2013 7:21 pm
That's what it does already.
Syntax: [ Download ] [ Hide ]
$variable = "a value";
$store = $variable;
$variable = "a different value";
echo $store; // a value
Assignments work by making a copy of the value, not by "referencing" the other variable.
I am still new with this but with Christopher code when referencing, does that mean making $variable and $store exactly the same and $store changes as soon as $variable changes and store the same new value?
Re: Variables
Posted: Wed Feb 20, 2013 2:00 pm
by woot
Yes, when a variable references another it is basically referring to the memory address of the first one and therefore when you change the value of $variable, $store will automatically change as well.
If it's not clear enough try taking a look at the PHP Manual
here but here's the example that explains what you want:
Assign By Reference
In the first of these, PHP references allow you to make two variables refer to the same content. Meaning, when you do:
it means that $a and $b point to the same content.
Note: $a and $b are completely equal here. $a is not pointing to $b or vice versa. $a and $b are pointing to the same place.
Re: Variables
Posted: Wed Feb 20, 2013 2:17 pm
by YoussefSiblini
Thank you, this cleared it for me
