Variables

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!

Moderator: General Moderators

Post Reply
YoussefSiblini
Forum Contributor
Posts: 206
Joined: Thu Jul 21, 2011 1:51 pm

Variables

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Variables

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Variables

Post 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!
(#10850)
YoussefSiblini
Forum Contributor
Posts: 206
Joined: Thu Jul 21, 2011 1:51 pm

Re: Variables

Post 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 :)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Variables

Post 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.
YoussefSiblini
Forum Contributor
Posts: 206
Joined: Thu Jul 21, 2011 1:51 pm

Re: Variables

Post 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?
User avatar
woot
Forum Newbie
Posts: 8
Joined: Tue Feb 19, 2013 8:35 am

Re: Variables

Post 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:

Code: Select all

<?php
$a =& $b;
?> 
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.
YoussefSiblini
Forum Contributor
Posts: 206
Joined: Thu Jul 21, 2011 1:51 pm

Re: Variables

Post by YoussefSiblini »

Thank you, this cleared it for me :)
Post Reply