カテゴリー
PHP

関数(値渡しと参照渡し)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta name="GENERATOR" content="JustSystems Homepage Builder Version 18.0.14.0 for Windows">
<title></title>
</head>
<body>
 <?php
function SampFuncNo1($vol) {
  print "値渡し<BR>";
  print "ポイント2 → $vol<BR>";
  $vol = $vol * 2;
  print "ポイント3 → $vol<BR>";
  return $vol;
}
function SampFuncNo2(&$a) {
  print "参照渡し<BR>";
  print "ポイント2 → $a<BR>";
  $a = $a * 2;
  print "ポイント3 → $a<BR>";
  return $a;
}

  $vol = 100;
  print "<HR>";
  print "ポイント1 → $vol<BR>";
  $ret = SampFuncNo1($vol);
  print "ポイント4 → $vol<BR>";

  $vol = 100;
  print "<HR>";
  print "ポイント1 → $vol<BR>";
  $ret = SampFuncNo2($vol);
  print "ポイント4 → $vol<BR>";

?>

<p><a href="index.php">トップページへ戻る</a></p>

</body>
</html>

 参照渡しを受ける方の変数名に&が付きますが、呼ぶ方は値渡しと参照渡しの違いは無いようです。あやふやな記憶でC#やC++とも違うようです。

inserted by FC2 system