メールフォームのための準備運動

練習問題1

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>$_GET:練習問題</title>
</head>
<body>
<form action="0820_ex01.php" method="GET">
1つめ<input type="text" name="1st">
2つめ<input type="text" name="2nd">
<input type="submit" value="並べる"></form>
</body>
</html>

0820_ex01.php

<?php
  $a = $_GET['1st'];
  $b = $_GET['2nd'];
  print ('文字列連結子で結合:'.$a . $b);
?>

練習問題2

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>$_GET:練習問題2</title>
</head>
<body>
<form action="0820_ex02.php" method="GET">
1つめ<input type="text" name="1st">
2つめ<input type="text" name="2nd">
<input type="submit" value="計算する"></form>
</body>
</html>

0820_ex02.php

<?php
  $a = $_GET['1st'];
  $b = $_GET['2nd'];
  echo'+で結合:';
  echo $a + $b.'<br>';
	print ('+で結合:'. $a + $b);//?>

※練習問題1で「print」使ったのでそのまま流用したら
 「$b」の数字しか表示しないという状態に。

練習問題2-1

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>$_GET:練習問題2</title>
</head>
<body>
<form action="0820_ex021.php" method="GET">
1つめ<input type="text" name="1st">
2つめ<input type="text" name="2nd">
<input type="submit" value="計算する"></form>
</body>
</html>

0820_ex021.php

<?php
  $a = $_GET['1st'];
  $b = $_GET['2nd'];
	print '+で結合:'. ($a + $b);
?>

シンタックスエラーはありません」と診断されたにもかかわらず、
正しい結果を得るためには丸括弧の開始位置が違っていたという結果に。

失敗→print ('+で結合:'. $a + $b);
成功→print '+で結合:'. ($a + $b);

失敗例はシングルクオートで囲んだ扱いになったようで。

まとめ

  • シングルクオート('')→入力値を変数に入れてくれない
  • ダブルクオート("")→入力値を変数に入れてくれる