Blog » 技术 » PHP变量的指针,在变量前加&号

PHP变量的指针,在变量前加&号

以下是手册中关于PHP变量的指针的例子,理解一下.
注意其中几行加粗的,变量的变化.

In response to the example by mdiricks.

Extending the example given by mdiricks, the following code provides an explains the concept of re-referencing that is involved in making a call to function foo with the prototype foo(& var):

<!-- C re-referenced -->
<?

$a
= 'eh'
;
$b = & $a;
// $b == 'eh'
$c = & $b;
// $c == 'eh'
$d = 'meh'
;

echo
"\$a = $a\n"
; //$a = eh
echo
"\$b = $b\n"
; //$b = eh
echo
"\$c = $c\n"
; //$c = eh
echo
"\$d = $d\n"
; //$d = meh

$c = & $d ;
// $c == 'meh'
echo "\n"
;

echo
"\$a = $a\n"
; //$a = eh
echo
"\$b = $b\n"
; //$b = eh
echo
"\$c = $c\n"
; //$c = meh
echo
"\$d = $d\n"
; //$d = meh

?>

<!-- Value of c changed -->
<?

$a
= 'eh'
;
$b = & $a;
// $b == 'eh'
$c = & $b;
// $c == 'eh'
$d = 'meh'
;

echo
"\$a = $a\n"
; //$a = eh
echo
"\$b = $b\n"
; //$b = eh
echo
"\$c = $c\n"
; //$c = eh
echo
"\$d = $d\n"
; //$d = meh

$c = 'meh' ;
// $c == 'meh'. And also, $a = $b == 'meh'
echo "\n"
;

echo
"\$a = $a\n"
; //$a = meh
echo
"\$b = $b\n"
; //$b = meh
echo
"\$c = $c\n"
; //$c = meh
echo
"\$d = $d\n"
; //$d = meh

?>

This results in the following o/p:
<!-- C re-referenced -->
$a = eh
$b = eh
$c = eh
$d = meh

$a = eh
$b = eh
$c = meh
$d = meh

<!-- Value of c changed -->
$a = eh
$b = eh
$c = eh
$d = meh

$a = meh
$b = meh
$c = meh
$d = meh

Technorati : , ,

Tags:

21andy.com相关文章

本文地址: http://www.21andy.com/blog/20060916/432.html

发表评论