Web Programming Language/PHP

call by value, reference

D4tai1 2018. 8. 21.

1. call by value

 

<?php	//함수 입력부분 call by value
	function swap($a,$b) {
		$temp = $a;
		$a = $b;
		$b = $temp;
	}

	$i=3;
	$j=4;

	print "Before : $i, $j 
"; swap($i, $j); print "After : $i, $j
"; ?>

 

 

2. call by reference

<?php 	//함수 입력부분 call by reference
	
	function swap(&$a, &$b) {
		$temp = $a;
		$a = $b;
		$b = temp;
	}

	$i=3;
	$j=4;

	print "Before : $i, $j 
"; swap($i, $j); print "After : $i, $j
"; ?>

'Web Programming Language > PHP' 카테고리의 다른 글

PHP 기본상식  (0) 2018.08.21
jquery[basic]  (0) 2018.08.21
class  (0) 2018.08.21
fopen, fput, fclose, fgets  (0) 2018.08.21
배열  (0) 2018.08.21

댓글