- array_search: documentation ( source)
- array_rand: documentation ( source)
- range: documentation ( source)
- printf: documentation ( source)
<?php
/* 問題 */
echo "ランダムに選出したアルファベットはH~Nの中に含まれているか?\n";
echo "またそのアルファベットは(Hから数えて)何番目のアルファベットか?\n";
echo "\n";
/* 変数の準備 */
$range = range('H', 'N'); // 限定する範囲
$all = range('A', 'Z'); // 全範囲
$rand = $all[array_rand($all)]; // ランダムに選出されたアルファベット
/* 判定と表示 */
$pos = array_search($rand, $range, true);
if ($pos !== false) {
printf("アルファベット[%s]は[%d]番目にありました\n", $rand, $pos);
} else {
printf("アルファベット[%s]は見つかりませんでした\n", $rand);
}