Functions III
PHP Code Examples
[Download]   [Execute]
<?php $title = 'Functions III'; include '../head1.inc'; 

// Reference parameters.
function alice(&$a, &$b)
{
	$a = $b + 4;
	$b += 7;
}
$x = 4;
$y = 5;
alice($x, $y);
echo "$x, $y<p>\n";

// Parameters may be sent by reference optionally.
function rabbit($a, $b)
{
	$a = $b + 4;
	$b += 7;
}
$x = 4;
$y = 5;
rabbit($x, $y); echo "$x, $y<br>\n";
rabbit(&$x, $y); echo "$x, $y<br>\n";
rabbit($x, &$y); echo "$x, $y<br>\n";
rabbit(&$x, &$y); echo "$x, $y<br>\n";
alice($x, &$y); echo "$x, $y<p>\n";

// Variables are local, unless declared otherwise.
function narble() 
{
	global $night;
	$night = 7;
	$day = 44;
}
$night = 100;
$day = 101;
echo "$night $day&nbsp;&nbsp;&nbsp;&nbsp;";
narble();
echo "$night $day<p>";

// Can return references.  References are reduced to ordinary data
// unless the & is used in the assignment.
$arr = array( 100, 101, 102, 103, 104, 105, 106 );
function &xyz($i)
{
	global $arr;
	return $arr[$i];
}
print_r($arr); echo "<br>";
$fred = xyz(2);
$fred = 4;
print_r($arr); echo "<br>";
$fred = &xyz(3);
$fred = 8;
print_r($arr); echo "<p>";
?>
</body>
</html>