ch08 / exam08-12.php

참조에 의한 호출 (Call by Reference) swap() 시각화

매개변수 앞의 & 기호(&$a, &$b)로 원본 변수의 메모리 주소를 직접 공유하여 swap 성공

PHP Source (ch08/exam08-12.php)
단계: 0 / 0
1-7<!DOCTYPE html> <html> ... <body>
8<?php
9function swap(&$a, &$b) { // 참조(Reference)로 수신
10$tmp = $a;
11$a = $b;
12$b = $tmp;
13}
15$num1 = 10;
16$num2 = 20;
17echo "swap(&$a, &$b) 실행 전 : $num1 = $num1, $num2 = $num2<br>";
19swap($num1, $num2);
20echo "swap(&$a, &$b) 실행 후 : $num1 = $num1, $num2 = $num2<br>";
21?>
22-23</body> </html>
준비 완료 실행 버튼 또는 다음 단계를 눌러주세요.
참조 포인터 메모리 구조 (&)
실제 메모리 셀 (Shared Storage)
[메모리 #01] $num1 & $a
10
[메모리 #02] $num2 & $b
20
$tmp 임시 저장값: - &$a → $num1, &$b → $num2 연결
http://localhost/ch08/exam08-12.php

참조에 의한 호출 (Call by Reference)