2013-08-25 43 views
-3

php docs是什麼,這些運營商之間的差異=和=

我發現了這一點,但弄得完全是什麼,這些運營商(= and =&)

$instance = new SimpleClass(); 

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

之間的區別這個正確任何人能解釋嗎?

+5

http://www.php.net/manual/en/language.references.whatare.php – 2013-08-25 05:37:53

+0

您也可以檢查出[此鏈接](http://stackoverflow.com/questions/879/php-variables-passed-by-value-or-by-reference) – orustammanapov

回答

0

你可以理解爲以下幾點:

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

這意味着

$實例具有值 「5」 將是空

$分配的值爲「5 「不會爲空,因爲它與」5「一起存儲。

$ reference的值爲「5」,因爲它指向$實例

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