youngfromnowhere

[PHP] php & operator : reference 연산자 본문

PHP&Apache2/PHP

[PHP] php & operator : reference 연산자

곽일땡 2023. 6. 26. 17:04

PHP의 & 연산자는 C언어의 포인터와 자주 비교되는데, 정확한 의미는

한 변수에 또 다른 변수명(alias)을 붙여주는 것이다.

따라서 PHP의 reference는 실제 memory address를 가리키는 C언어의 포인터보다는

C++의 reference에 더 가까운 개념이다.

 

https://youngnowhere.tistory.com/43

 

[Java] Call-by-Value/Call-by-Address/Call-by-Reference

함수를 호출할 때 함수에 '무엇을' 전달하느냐에 따라 함수 호출방식을 Call-by-Value, Call-by-Address, Call-by-Reference로 나눈다. Call-by-Value. 함수에 어떤 변수의 값을 전달한다. 함수는 값을 전달받았을

youngnowhere.tistory.com

 C++에서 변수를 선언할 때 "address operator &"을 이용해 선언하면, 그것은 해당 변수명을 다른 변수의 또 다른 변수명(alias)으로 쓰겠다는 의미가 된다.

https://stackoverflow.com/questions/2209934/php-operator

 

PHP "&" operator

I'm not a PHP programmer (but know other languages), and I'm trying to understand a web page that was done in PHP (5.1.6) in order to do some changes. The page has the following code (simplified): $

stackoverflow.com

Instead, all PHP variables are actually references to an internal type called a zval. You cannot directly manipulate zvals in PHP, and nor can you make extra layers of indirection - every variable is a reference to a zval, and that's it. (See caveat: objects below.)
What an assignment-by-reference ($foo =& $bar), a pass-by-reference (function foo(&$bar) { ... }), or a return-by-reference (return &$foo) do is tell PHP that you want two variables to point at the same zval. Note that you are not pointing one variable "at" another - they are both equally "real", and calling unset() on either will leave the other completely untouched.

https://www.php.net/manual/en/language.references.whatare.php

 

PHP: What References Are - Manual

The following three code snippets show the effect of using references in scalar variables, arrays and objects under different circumstances.In any case the result is the expected one if you stick to the concept that a reference is an alias to a variable. A

www.php.net

References in PHP are a means to access the same variable content by different names. They are not like C pointers; for instance, you cannot perform pointer arithmetic using them, they are not actual memory addresses, and so on.

 

사실 이 주제는 php의 foreach statement를 공부하다가 튀어나온 것이다. 다음 예제를 보자.

https://www.php.net/manual/en/control-structures.foreach.php

 

$value is : 1 
now $value is : 2 
$value is : 2 
now $value is : 4 
$value is : 3 
now $value is : 6 
$value is : 4 
now $value is : 8 
Array
(
    [0] => 2
    [1] => 4
    [2] => 6
    [3] => 8
)

===========================
$key is : 0 
$value is : 2 
Array
(
    [0] => 2
    [1] => 4
    [2] => 6
    [3] => 2
)
$key is : 1 
$value is : 4 
Array
(
    [0] => 2
    [1] => 4
    [2] => 6
    [3] => 4
)
$key is : 2 
$value is : 6 
Array
(
    [0] => 2
    [1] => 4
    [2] => 6
    [3] => 6
)
$key is : 3 
$value is : 6 
Array
(
    [0] => 2
    [1] => 4
    [2] => 6
    [3] => 6
)

첫번째 foreach loop를 보면 $value 앞에 &를 붙였으므로(line3), $value는 loop를 돌면서 $arr[0], $arr[1], ...의 reference가 된다.

loop를 빠져나온후 unset()을 이용해 reference를 해제해주지 않았으므로 loop를 빠져나와서도 $value는 $arr[3]의 reference로 사용된다.

 

이제 $arr는 array(2, 4, 6, 8)인 상태로 두 번째 foreach loop를 탄다. (line15)

첫번째 loop에서 $key에는 0, $value에는 2가 전달된다.

이 때 $value는 $arr[3]을 가리키므로 $arr[3]의 value가 2로 바뀐다.

따라서 결과는 array(2, 4, 6, 2)

두번쨰 loop에서 $key에는 1, $value에는 4가 전달된다.

여전히 $value는 $arr[3]을 가리키므로 $arr[3]은 4가 되고

결과는 array(2, 4, 6, 4)

같은 과정으로 세번째 loop은 array(2, 4, 6, 6), 네번째 loop에서는 변화 없이 array(2, 4, 6, 6)이 된다.