前提・実現したいこと
PHPでじゃんけんゲームを作っています.
現在,自分の手を選ぶ前にすでにPCの手が出力されてしまっている状態です.
また自分の手を選んで送信すると結果の勝ち負けあいこの判定も出ていない状態です.
自分の手をラジオボタンで選択し,送信を押すとPCの手とじゃんけん結果が表示されるようにしたいです.
ご教授よろしくお願い致します.
発生している問題・エラーメッセージ
グー,チョキ,パーを選んで送信すると以下のエラーが出ます.
Warning: array_rand() expects parameter 1 to be array, string given in /Applications/XAMPP/xamppfiles/htdocs/php_basic/intermediate7_1.php on line 22 Notice: String offset cast occurred in /Applications/XAMPP/xamppfiles/htdocs/php_basic/intermediate7_1.php on line 22
該当のソースコード
php
1<?php 2$hand=array('グー', 'チョキ', 'パー'); 3$my=''; 4$pc=''; 5$result=''; 6 7if(isset($_POST['hand'])===TRUE){ 8 $hand=htmlspecialchars($_POST['hand'], ENT_QUOTES, 'UTF-8'); 9} 10 11// if($hand==='グー'){ 12// $my='<p>グー</p>'; 13// }else if($hand==='チョキ'){ 14// $my='<p>チョキ</p>'; 15// }else if($hand==='パー'){ 16// $my='<p>パー</p>'; 17// } 18 19if($my!==''){ 20 $pc='<p></p>'; 21}else{ 22 $pc=$hand[array_rand($hand)]; 23} 24 25if($my==='グー'&& $pc==='グー'){ 26 $pc='<p>グー</p>'; 27 $result='<p>あいこ</p>'; 28}else if($my==='グー' && $pc==='チョキ'){ 29 $pc='<p>チョキ</p>'; 30 $result='<p>勝ち</p>'; 31}else if($my==='グー' && $pc==='パー'){ 32 $pc='<p>パー</p>'; 33 $result='<p>負け</p>'; 34}else if($my==='チョキ' && $pc==='グー'){ 35 $pc='<p>グー</p>'; 36 $result='<p>負け</p>'; 37}else if($my==='チョキ' && $pc==='チョキ'){ 38 $pc='<p>チョキ</p>'; 39 $result='<p>あいこ</p>'; 40}else if($my==='チョキ' && $pc==='パー'){ 41 $pc='<p>パー</p>'; 42 $result='<p> 勝ち</p>'; 43}else if($my==='パー' && $pc='グー'){ 44 $pc='<p>グー</p>'; 45 $result='<p>勝ち</p>'; 46}else if($my==='パー' && $pc==='チョキ'){ 47 $pc='<p>チョキ</p>'; 48 $result='<p>負け</p>'; 49}else if($my==='パー' && $pc==='パー'){ 50 $pc='<p>パー</p>'; 51 $result='<p>あいこ</p>'; 52} 53 ?> 54 55 <!DOCTYPE html> 56 <html lang="ja"> 57 <head> 58 <meta charset="utf-8" /> 59 <title>じゃんけん</title> 60 </head> 61 <body> 62 <!-- <h1>じゃんけん勝負</h1> --> 63 <label>自分: <?php print $my; ?></label> 64 <label>相手: <?php print $pc; ?></label> 65 <label>結果: <?php print $result; ?></label> 66 67 <form method="post"> 68 <input type="radio" name="hand" value="グー" 69<?php if($hand==='グー'){ print "checked";} ?>/>グー 70 <input type="radio" name="hand" value="チョキ" 71<?php if($hand==='チョキ'){ print "checked";} ?>/>チョキ 72 <input type="radio" name="hand" value="パー" 73<?php if($hand==='パー'){ print "checked";} ?>/>パー 74 <input type="submit" value="勝負" /> 75 </form> 76 77 </body> 78 </html> 79
試したこと
PCの手の判定で自分の手が選ばれていなければ空白という表示にしてみたのですが,何も変わりませんでした.
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/06/10 07:16