4

I find that the reference in the code below is confusing,

$a = 4;
$b = &$a;
var_dump($b);

$a = 10;
var_dump($b); // 10

$b = 100;
var_dump($a); // 100 but shouldn't it be 10?

Value of $b is a reference to $a and $a is never a reference to $b.

But why when I change the value of $b. The value of $a changes as well?

EM-Creations
  • 4,195
  • 4
  • 40
  • 56
Run
  • 54,938
  • 169
  • 450
  • 748
  • 1
    I'm also confused but I think it's because of "C programming". In C, it would work as you expect, but in PHP, it may be different... In C, "$b" would just contain the address of "$a", so if you change "$b", it won't contain anymore the address of "$a"... – Random Jul 16 '15 at 07:35
  • that's what I thought it should be in PHP! It is not obviously! – Run Jul 16 '15 at 07:36
  • 1
    It must be because there are no types in PHP... In C, you add "*" to the type, to say it will be a pointer. here, when you declare "$b", it will be a variable with the same address of "$a", not a variable containing the address of "$a"... – Random Jul 16 '15 at 07:38

2 Answers2

4

In the line: $b = &$a; The variable $b is being set up as a reference to $a (as in it will point to the same memory location as $b). In this respect $b essentially becomes an alias or another way of accessing and modifying $a.

This link explains pointers in C++ (it's important to stress that this is not C++ but the link explains pointers / references well).

I hope this helps.

EM-Creations
  • 4,195
  • 4
  • 40
  • 56
  • 1
    so they are pointing to each other actually in another words - in PHP? – Run Jul 16 '15 at 07:35
  • They're not pointing to each other, `$b` is pointing to `$a`. In memory `$a` would hold the value of 100 and `$b` would hold the value of `$a`'s memory address. It literally "points" to `$a`. – EM-Creations Jul 16 '15 at 07:37
  • why would $100 holds the value of 100? it is not pointing to anything, isn't? – Run Jul 16 '15 at 07:38
  • @EM-Creations Are you sure it works as in C++ ? I think in fact the confusion comes from C/C++, since it doesn't work the same way... – Random Jul 16 '15 at 07:40
  • I'm referencing the last assignment. At the end of the program's execution, `$a` would hold the value of 100. This is because `$b` is essentially an alias (another name) for `$a` programmatically after you set it up as a reference / pointer to `$a`. – EM-Creations Jul 16 '15 at 07:41
  • @Random I didn't say it works the same as in C++, I just added the link as it explains what pointers are. – EM-Creations Jul 16 '15 at 07:43
  • 1
    But I point that the OP seems to know exactly how pointers work in C/C++, and this is why he is confused... – Random Jul 16 '15 at 08:14
  • 1
    Upvoted for the use of `alias`. It is a really good way to define references in PHP. – tleb Jul 16 '15 at 08:41
1

$b = &$a; represents their values are equal if you define $b then $a value becomes equal to that of $b respectively.

Check out this link for more information

Malik Naik
  • 1,472
  • 14
  • 16