カテゴリー
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 background="y8b.gif">

 <?php
  require_once ("newpage8.php");
//自身の環境に合わせて書き換える
//接続に必要な$hostname、$dbname、$username、$passwordはnewpage8.phpへ移動
//newpage8.phpはサイト転送出来ないので、転送ツールでアップする

  print $APPTITLE;

  MyTitlePrint("実験開始!データベース接続テスト");

//------
$dsn = "mysql:host=".$hostname.";dbname=".$dbname;

try {
$dbh = new PDO($dsn, $username, $password);

   // 接続を使用する
    $sql = 'select id, namae, jyusyo from meibo';
    $stmt= $dbh->query( $sql);

} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}

  print "$APPFOOTER";

?>

<p><a href="newpage10.php">書き込みインデックスへ戻る</a></p>
</body>
</html>

データベースに接続できました。良く分からないのですが、MySQLユーザを元に戻しました。削除してしまったかと思ってましたが、実はあって2個ユーザが登録になってました。もとのMySQLユーザに戻したらあっさり接続できました。

この場合は$stmtは使わないので、関係ないと思いますが、表形式でプリントする場合、必要でしたがここでも訳の分からないエラーが出ました。$の前に全角のスペースがあると見た目は分からないのですが、エラーになるようです。

カテゴリー
PHP

フォーム部品とPHP

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<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>フォーム部品とPHP</TITLE>
</HEAD>
<body background="y8b.gif">

 <?php
  require_once ("newpage8.php");
  print $APPTITLE;
  MyTitlePrint("実験開始!入力部品フォーム 表示!!");
?>

<FORM method="POST" action="smp2-92.php">
<TABLE border="0">
  <TBODY>
    <TR>
      <TD>テキストボックス</TD>
      <TD>
        <INPUT size="35" type="text" name="textbox">
      </TD>
    </TR>
    <TR>
      <TD>テキストエリア</TD>
      <TD>
        <TEXTAREA rows="4" cols="24" name="textarea"></TEXTAREA>
      </TD>
    </TR>
    <TR>
      <TD>ラジオボタン</TD>
      <TD>
        <INPUT type="radio" name="radiobutton" value="1">選択肢1
        <INPUT type="radio" name="radiobutton" value="2">選択肢2
        <INPUT type="radio" name="radiobutton" value="3">選択肢3
      </TD>
    </TR>
    <TR>
      <TD>チェックボックス</TD>
      <TD>
        <INPUT type="checkbox" name="checkbox" value="1">
      </TD>
    </TR>
    <TR>
      <TD>リストボックス</TD>
      <TD>
        <SELECT size="4" name="listbox">
          <OPTION value="1">選択肢1</OPTION>
          <OPTION value="2">選択肢2</OPTION>
          <OPTION value="3">選択肢3</OPTION>
          <OPTION value="4">選択肢4</OPTION>
        </SELECT>
      </TD>
    </TR>
    <TR>
      <TD>オプションメニュー</TD>
      <TD>
        <SELECT name="optionmenu">
          <OPTION value="1">選択肢1</OPTION>
          <OPTION value="2">選択肢2</OPTION>
          <OPTION value="3">選択肢3</OPTION>
          <OPTION value="4">選択肢4</OPTION>
        </SELECT>
      </TD>
    </TR>
    <TR>
      <TD>隠しフィールド</TD>
      <TD>
        <INPUT type="hidden" name="secret" value="秘密のデータ">
      </TD>
    </TR>
    <TR>
      <TD colspan="2" align="center"><BR>
        <INPUT type="submit" value=" OK  " name="okbutton">
      </TD>
    </TR>
  </TBODY>
</TABLE>
</FORM>

<p><a href="newpage10.php">書き込みインデックスへ戻る</a></p>
</body></html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<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>フォーム部品とPHP</TITLE>
</HEAD>

<body background="y8b.gif">

 <?php
  require_once ("newpage8.php");
  print $APPTITLE;
  MyTitlePrint("実験開始!入力部品 結果表示!!");
?>
<?php
if (isset($_POST['okbutton'])) { ?>

<TABLE border="1">
  <TBODY>
    <TR>
      <TD>テキストボックス</TD>
      <TD><?php
            print $_POST["textbox"]; ?> </TD>
    </TR>
    <TR>
      <TD>テキストエリア</TD>
      <TD><?php
            print $_POST["textarea"]; ?></TD>
    </TR>
    <TR>
      <TD>ラジオボタン</TD>
      <TD><?php
            print $_POST["radiobutton"]; ?></TD>
    </TR>
    <TR>
      <TD>チェックボックス</TD>
      <TD><?php
            print $_POST["checkbox"]; ?></TD>
    </TR>
    <TR>
      <TD>リストボックス</TD>
      <TD><?php
            print $_POST["listbox"]; ?></TD>
    </TR>
    <TR>
      <TD>オプションメニュー</TD>
      <TD><?php
            print $_POST["optionmenu"]; ?></TD>
    </TR>
    <TR>
      <TD>プッシュボタン</TD>
      <TD><?php
            print $_POST["button"]; ?></TD>
    </TR>
    <TR>
      <TD>隠しフィールド</TD>
      <TD><?php
            print $_POST["secret"]; ?></TD>
    </TR>
  </TBODY>
</TABLE>
<?php
print "$APPFOOTER";
}
?>

<p><a href="newpage10.php">書き込みインデックスへ戻る</a></p>
</body></html>

PHPでは”と’の違いがあるのか?無いのか?混同して使ってます。

カテゴリー
PHP

フォームとPHPのやりとり

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<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 background="y8b.gif">

 <?php
  require_once ("newpage8.php");
  print $APPTITLE;
  MyTitlePrint("実験開始!入力フォーム 表示!!");
?>

<FORM method="POST" action="smp2-89.php">
<TABLE border="0">
  <TBODY>
    <TR>
      <TD>氏名</TD>
      <TD><INPUT size="35" type="text" name = "username"></TD>
    </TR>
    <TR>
      <TD>郵便番号</TD>
      <TD><INPUT size="14" type="text" name="zip"></TD>
    </TR>
    <TR>
      <TD>住所</TD>
      <TD><INPUT size="50" type="text" name="address"></TD>
    </TR>
    <TR>
      <TD>電話番号</TD>
      <TD><INPUT size="35" type="text" name="tel"></TD>
    </TR>
    <TR>
      <TD>メールアドレス</TD>
      <TD><INPUT size="35" type="text" name="email"></TD>
    </TR>
    <TR>
      <TD>性別</TD>
      <TD><INPUT type="radio" name="sexes" value="男">男 <INPUT type="radio" name="sexes" value="女">女 </TD>
    </TR>
    <TR>
      <TD colspan="2" align="center"><BR>
      <INPUT type="submit" value=" 登録 " name="exereg"> <INPUT type="submit" value="キャンセル" name="cancelreg"></TD>
    </TR>
  </TBODY>
</TABLE>
</FORM>

<?php
  print "$APPFOOTER";
?>

<p><a href="newpage10.php">書き込みインデックスへ戻る</a></p>
</body></html>
<!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

  if (isset($_POST['exereg'])) {
    print "登録ボタンがクリックされました!" . "<BR>";
    print "氏名 → "            . $_POST["username"] . "<BR>";
    print "郵便番号 → "        . $_POST["zip"] . "<BR>";
    print "住所 → "            . $_POST["address"] . "<BR>";
    print "電話番号 → "        . $_POST["tel"] . "<BR>";
    print "メールアドレス → "  . $_POST["email"] . "<BR>";
    print "性別 → "            . $_POST["sexes"] . "<BR>";

  }
  elseif (isset($_POST['cancelreg'])) {
    print "キャンセルボタンがクリックされました!";
  }

?>

</body>
</html>

フォームの方の記述は上の部分ですが、「ネタ本」(PHP5.0とMySQL5.0で…)の通りですが、PHPの部分は本の通りでなく、変わってます。理由は分かりません。本の内容が間違っているのか?PHPの使用が変更になってしまたのか?いずれにしても、例えば$usernameでは駄目で、$_POST[”username”]の様で上手く行きました。あと登録ボタンの判定を最初にしてから、printするようにしました。

カテゴリー
PHP

書き込みインデックス(newpage10.php)

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="GENERATOR" content="JustSystems Homepage Builder Version 18.0.14.0 for Windows">
<title>山爺のPHP試行錯誤実験室</title>
<link rel="stylesheet" type="text/css" href="table.css" id="HPB_TABLE_CSS_ID_">
</head>
<body background="y8b.gif">
<p><img src="logo12.gif" width="377" height="45" border="0" alt="山爺のPHP試行錯誤実験室"></p>
<p>ホームページビルダーVer18でPHPの試行錯誤の実験を行っていきます。</p>
<table border="1" width="100%" cellspacing="0" cellpadding="2" id="HPB_TABLE_5_A_200520105336" class="hpb-cnt-tb1">
  <tbody>
    <tr>
      <th class="hpb-cnt-tb-th1" width="200">言語の種類</th>
      <th class="hpb-cnt-tb-th1" width="384">実行の名称</th>
      <th class="hpb-cnt-tb-th1" width="230">参考ブログ</th>
    </tr>
    <tr>
      <td class="hpb-cnt-tb-cell1" height="47" width="211" align="center">PHP_No1</td>
      <td class="hpb-cnt-tb-cell2" height="47" width="384" align="center">トップページ</td>
      <td class="hpb-cnt-tb-cell1" height="47" align="center"><a href="http://isamrx72.wpblog.jp/2020/05/20/%e5%b1%b1%e7%88%ba%e3%81%aephp%e8%a9%a6%e8%a1%8c%e9%8c%af%e8%aa%a4%e5%ae%9f%e9%a8%93%e5%ae%a4%e3%81%aeindex-php/">index.php</a></td>
    </tr>
    <tr>
      <td class="hpb-cnt-tb-cell1" height="47" width="211" align="center"></td>
      <td class="hpb-cnt-tb-cell2" height="47" width="384" align="center">書き込みインデックス</td>
      <td class="hpb-cnt-tb-cell1" height="47" align="center">newpage10.php</td>
    </tr>
  <tr>
      <td class="hpb-cnt-tb-cell1" height="47" width="211" align="center">newpage2.php</td>
      <td class="hpb-cnt-tb-cell2" height="47" width="384" align="center"><a href="newpage2.php">今日の日付と一致するか?</a></td>
      <td class="hpb-cnt-tb-cell1" height="47" align="center"><a href="http://isamrx72.wpblog.jp/2020/05/18/php%e8%a9%a6%e8%a1%8c%e9%8c%af%e8%aa%a4%e3%81%ae%e6%97%a5%e8%a8%98%e3%81%9d%e3%81%ae1/">f文の例</a></td>
    </tr>

    <tr>
      <td class="hpb-cnt-tb-cell1" height="47" width="211" align="center">newpage1.php</td>
      <td class="hpb-cnt-tb-cell2" height="47" width="384" align="center"><a href="newpage1.php">今日の日付</a></td>
      <td class="hpb-cnt-tb-cell1" height="47" align="center"><a href="http://isamrx72.wpblog.jp/2020/05/20/%e4%bb%8a%e6%97%a5%e3%81%ae%e6%97%a5%e4%bb%98%e3%80%81date%e9%96%a2%e6%95%b0/">date関数</a></td>
    </tr>
    <tr>
      <td class="hpb-cnt-tb-cell1" height="47" width="211" align="center">newpage3.php</td>
      <td class="hpb-cnt-tb-cell2" height="47" width="384" align="center"><a href="newpage3.php">100以下の2の階乗</a></td>
      <td class="hpb-cnt-tb-cell1" height="47" align="center"><a href="http://isamrx72.wpblog.jp/2020/05/19/php%e8%a9%a6%e8%a1%8c%e9%8c%af%e8%aa%a4%e5%ae%9f%e9%a8%93%e5%ae%a4%e3%81%9d%e3%81%ae2/">for文、if文(break)の使用例</a></td>
    </tr>
    <tr>
      <td class="hpb-cnt-tb-cell1" height="47" width="211" align="center">newpage4.php</td>
      <td class="hpb-cnt-tb-cell2" height="47" width="384" align="center"><a href="newpage4.php">2バイト文字列関数</a></td>
      <td class="hpb-cnt-tb-cell1" height="47" align="center"><a href="http://isamrx72.wpblog.jp/2020/05/20/mb_convert_encoding%e9%96%a2%e6%95%b0%e3%81%ae%e4%bd%bf%e7%94%a8%e4%be%8b/">mb_convert_encoding関数</a></td>
    </tr>
    <tr>
      <td class="hpb-cnt-tb-cell1" height="47" width="211" align="center">newpage5.php</td>
      <td class="hpb-cnt-tb-cell2" height="47" width="384" align="center"><a href="newpage5.php">関数の使用例</a></td>
      <td class="hpb-cnt-tb-cell1" height="47" align="center"><a href="http://isamrx72.wpblog.jp/2020/05/21/%e9%96%a2%e6%95%b0%e3%81%ae%e4%bd%9c%e6%88%90%e4%be%8b/">関数での&lt;FONT&gt;の使い方</a></td>
    </tr>
    <tr>
      <td class="hpb-cnt-tb-cell1" height="47" width="211" align="center">newpage6.php</td>
      <td class="hpb-cnt-tb-cell2" height="47" width="384" align="center"><a href="newpage6.php">関数(値渡しと参照渡し)</a></td>
      <td class="hpb-cnt-tb-cell1" height="47" align="center"><a href="http://isamrx72.wpblog.jp/2020/05/23/%e5%a4%89%e6%95%b0%e3%81%ae%e3%82%a4%e3%83%b3%e3%82%af%e3%83%ab%e3%83%bc%e3%83%89/">例題</a></td>
    </tr>
    <tr>
      <td class="hpb-cnt-tb-cell1" height="47" width="211" align="center">newpage7.php,newpage8.php</td>
      <td class="hpb-cnt-tb-cell2" height="47" width="384" align="center">newpage7.phpとnewpage8.phpで関数と変数をインクルード</td>
      <td class="hpb-cnt-tb-cell1" height="47" align="center"><a href="http://isamrx72.wpblog.jp/2020/05/23/%e9%96%a2%e6%95%b0%e3%81%ae%e3%82%a4%e3%83%b3%e3%82%af%e3%83%ab%e3%83%bc%e3%83%89/">関数のインクルード</a></td>
    </tr>
    <tr>
      <td class="hpb-cnt-tb-cell1" height="47" width="211" align="center">newpgage9.php</td>
      <td class="hpb-cnt-tb-cell2" height="47" width="384" align="center"><a href="newpage9.php">ページを作成する例</a></td>
      <td class="hpb-cnt-tb-cell1" height="47" align="center"><a href="http://isamrx72.wpblog.jp/2020/05/25/php%e3%81%a7html%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%82%92%e4%bd%9c%e3%82%8b/">phpでhtml形式を作る</a></td>
    </tr>
   <tr>
      <td class="hpb-cnt-tb-cell1" height="47" width="211" align="center">newpage11.php</td>
      <td class="hpb-cnt-tb-cell2" height="47" width="384" align="center"><a href="newpage11.php">書き込みテンプレートへリンク</a></td>
      <td class="hpb-cnt-tb-cell1" height="47" align="center"><a href="http://isamrx72.wpblog.jp/2020/05/25/%e5%ae%9f%e8%a1%8c%e6%9b%b8%e3%81%8d%e8%be%bc%e3%81%bf%e3%81%ae%e3%83%86%e3%83%b3%e3%83%97%e3%83%ac%e3%83%bc%e3%83%88/">テンプレート表示</a></td>
    </tr>
   <tr>
      <td class="hpb-cnt-tb-cell1" height="47" width="211" align="center">newpage12.php</td>
      <td class="hpb-cnt-tb-cell2" height="47" width="384" align="center"><a href="newpage12.php">配列の使い方へリンク</a></td>
      <td class="hpb-cnt-tb-cell1" height="47" align="center"><a href="http://isamrx72.wpblog.jp/2020/05/25/%e9%85%8d%e5%88%97%e3%81%ae%e4%bd%bf%e3%81%84%e6%96%b9/">配列の使い方表示</a></td>
    </tr>
  <tr>
      <td class="hpb-cnt-tb-cell1" height="47" width="211" align="center"></td>
      <td class="hpb-cnt-tb-cell2" height="47" width="384" align="center"></td>
      <td class="hpb-cnt-tb-cell1" height="47" align="center"></td>
    </tr>


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

今時点の書き込みのインデックスなので、続けて書いていけば、増えるの変わります。構造は同じです。

カテゴリー
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
  require_once ("newpage8.php");
  print $APPTITLE;

  $room[0] = "鈴木";
  $room[1] = "佐藤";
  $room[2] = "伊藤";
  $room[3] = "高橋";
  for($i = 0; $i<4; $i++){
    print $room[$i] . "<BR>";
  }

  print "$APPFOOTER";

?>

<p><a href="newpage10.php">書き込みインデックスへ戻る</a></p>
</body>
</html>

変数名に$が付くようです。タイプは無いようですね。

カテゴリー
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
  require_once ("newpage8.php");
  print $APPTITLE;


  print "$APPFOOTER";

?>

<p><a href="newpage10.php">書き込みインデックスへ戻る</a></p>
</body>
</html>

phpのコードは上のブロックの二つのprintの間に書きます。

カテゴリー
PHP

phpでhtmlファイルを作る

<!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
require_once("newpage7.php");
print $HTMLHEADER;
print $APPTITLE;
print "<BR>悩んでます??";
print $HTMLFOOTER;
?>
</body>
</html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="GENERATOR" content="JustSystems Homepage Builder Version 18.0.14.0 for Windows">
<?php
  $APPTITLE = "試行錯誤試験中です!";

  $HTMLHEADER = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
  <HTML>
  <HEAD>
  <META http-equiv='Content-Type' content='text/html; charset=Shift_JIS'>
  <META http-equiv='Content-Style-Type' content='text/css'>
  <TITLE>" . $APPTITLE . "</TITLE>
  </HEAD>
  <BODY>
  <A href='newpage10.php'>書き込みインデックスへ戻る</A>
";

  $HTMLFOOTER= "</body>
                </html>";
?>

上のブロックが実際にhtmlを作ってますが、書式のみはそうですが、実際にファイルがあるわけでもないようです。下のブロックでは上で使うグローバルな変数を用意してます。下のphpファイルの名前はnewpage7.phpですが、リンクの状態を表示では、表示されません。また、転送すべきファイルにもありません。強制的にエディタで開いて、訂正出来ましたが、phpとの関連ではうまくないようですが、この場合は上手く行ったようです。

カテゴリー
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
  require_once ("newpage8.php");
  print $APPTITLE;

function SampFuncNo1() {
  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;
}

  MyTitlePrint("実験開始!関数(値渡しと参照渡し)");
  $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>
<!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 MyTitlePrint($str){
    print "<H2>$str</H2>";
    return;
  }
  function fontcolor($moji, $color) {
    $a = "<B><FONT color='$color'>" . $moji . "</FONT></B>";
    return $a;
  }

  $a = "<H2>山爺の";
  $a .= fontcolor("PHP", "#6666FF");
  $a .= fontcolor("試行錯誤的実験室", "FF0000") . "</H2><BR>";
  $APPTITLE = $a;

  ?>
</body>
</html>

前にアップした時と違ってきているので、newpage8.php関数の値渡しと参照渡し例を再度表示します。

カテゴリー
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 MyTitlePrint($str){
  print "<H2>$str</H2>";
  return;
}
?>
</body>
</html>

上は関数です。

<!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
require_once("newpage7.php");
function fontcolor($moji, $color) {
  $a = "<B><FONT color='$color'>" . $moji . "</FONT></B>";
  return $a; 
}

  MyTitlePrint("山爺の実験開始!関数(スクリプトとHTMLの混在");
  fontcolor("PHP", "#FF0000");
  print "<BR>";
  $a = "PHPはスクリプトと";
  $a .= fontcolor("HTMLを混在できます!", "#6666FF" . "<BR>");
  print $a;

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

</body>
</html>

 上は関数を呼ぶ側です。newpage7.phpはindex.phpと同じディレクトリーに入れてあることが条件です。require_once(“newpage7.php”);と書くことによって、MyTitlePrint(“xxx”);をここで定義した関数fontcolorと同じように呼び出せるようです。

カテゴリー
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