<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>配列からからラジオボタンを生成したい</title>
</head>
<body>
<form method="post" action="">
<?php
if (isset($_POST['sex'])) {
echo '<p>性別は: ' . h($_POST['sex']) . 'です。</p>';
}
$array = array('男性', '女性');
echo '<p>性別を選択してください</p>';
foreach ($array as $value) {
if ($value == @$_POST['sex']) {
echo '<input type="radio" name="sex" value="' . h($value) . '" checked="checked" />' . h($value);
} else {
echo '<input type="radio" name="sex" value="' . h($value) . '" />' . h($value);
}
}
function h($string) {
return htmlspecialchars($string, ENT_QUOTES);
}
?>
<br />
<input type="submit" value="送信する" />
</form>
</body>
</html>