PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?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?
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!
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
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
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?
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.