-3

From php docs

I found this but confused totally what are the difference between these operators (= and =&)

$instance = new SimpleClass();

$assigned   =  $instance;
$reference  =& $instance;

Can anyone explain about this properly, please?

Navin Rauniyar
  • 10,127
  • 14
  • 45
  • 68

2 Answers2

1
    <?php
    $a = 1;
    $b = $a;
    $b = 2;
    echo "a:{$a} / b: {$b}<br />";
    // returns 1/2

    $a = 1;
    $b =& $a;
    $b = 2;
    echo "a:{$a} / b: {$b}<br />";
    // returns 2/2
     ?>
Above example clarifies the difference
mahendra
  • 133
  • 3
0

You may understood as following:

$instance = "5";
$assigned = $instance; // stores "5"
$reference =& $instance; // Point to the object $instance stores "5"
$instance = null; // $instance and $reference become null

Which means

$instance has the value "5" would be null

$assigned has the value "5" wouldn't be null coz it is stored with "5"

$reference has the value "5" would be null as it is pointed to $instance

Community
  • 1
  • 1
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231