質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

解決済

2回答

498閲覧

php 初回出力時、エラーが出てしまうので解消したい。

YTKM

総合スコア22

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

1クリップ

投稿2022/07/01 10:52

やりたい事

ジャンケンプログラムのデバックをしています。
1勝目の場合のみエラーが出てしまうので解消したいです。
2勝目、3勝目からは正常に蓄積されていきます。

ブラウザ

Image from Gyazo

Image from Gyazo

試した事

初回処理と2回目以降の処理の切り分けが出来ていないと思い

php

1if (! empty($_SESSION['result'])) { 2 $_SESSION['result'] = 0; 3}

! empty を記述して、要素が定義されているか確認しましたが変わらず。
初歩的な質問で大変申し訳ございません。
クラス・継承など勉強不足なので改めて勉強します。
分かる方居ましたら宜しくお願い致します。

コード

php

1<?php 2// デバック練習問題 3// あらかじめ壊れているプログラムを用意してあります。 4// コードを読みデバックしつつジャンケンゲームを完成させてください。 5// 判定が勝った時のみ勝利回数を表示させてください。 6// 例) 7// 山田太郎はグーを出しました。 8// 相手はチョキを出しました。 9// 勝敗は勝ちです。 10// 3回目の勝利です。 11 12 13if (! empty($_SESSION['result'])) { 14 $_SESSION['result'] = 0; 15} 16 17class Player 18{ 19 public function jankenConverter(int $choice): string 20 { 21 $janken = ''; 22 switch ($choice) { 23 case 1: 24 $janken = 'グー'; 25 break; 26 case 2: 27 $janken = 'チョキ'; 28 break; 29 case 3: 30 $janken = 'パー'; 31 break; 32 default: 33 } 34 return $janken; 35 } 36} 37 38class Me extends Player 39{ 40 private $name; 41 private $choice; 42 43 public function __construct(string $lastName, string $firstName, int $choice) 44 { 45 $this->name = $lastName.$firstName; 46 $this->choice = $choice; 47 } 48 49 public function getName(): string 50 { 51 return $this->name; 52 } 53 54 public function getChoice(): string 55 { 56 return $this->jankenConverter($this->choice); 57 } 58} 59 60class Enemy extends Player 61{ 62 private $choice; 63 public function __construct() 64 { 65 $this->choice = random_int(1, 3); 66 } 67 68 public function getChoice(): string 69 { 70 return $this->jankenConverter($this->choice); 71 } 72} 73 74class Battle 75{ 76 private $first; 77 private $second; 78 public function __construct(Me $me, Enemy $enemy) 79 { 80 $this->first = $me->getChoice(); 81 $this->second = $enemy->getChoice(); 82 } 83 84 public function judge(): string 85 { 86 if ($this->first === $this->second) { 87 return '引き分け'; 88 } 89 90 if ($this->first === 'グー' && $this->second === 'チョキ') { 91 return '勝ち'; 92 } 93 94 if ($this->first === 'グー' && $this->second === 'パー') { 95 return '負け'; 96 } 97 98 if ($this->first === 'チョキ' && $this->second === 'グー') { 99 return '負け'; 100 } 101 102 if ($this->first === 'チョキ' && $this->second === 'パー') { 103 return '勝ち'; 104 } 105 106 if ($this->first === 'パー' && $this->second === 'グー') { 107 return '勝ち'; 108 } 109 110 if ($this->first === 'パー' && $this->second === 'チョキ') { 111 return '負け'; 112 } 113 } 114 115 public function countVictories() 116 { 117 session_start(); 118 if (($this->judge() === '勝ち')) { 119 // $_SESSION = array(); 120 // session_destroy(); 121 $_SESSION['result'] += 1; 122 } 123 return $_SESSION['result']; 124 } 125 126 // public function getVictories() 127 // { 128 // return $_SESSION['result']; 129 // } 130 131 public function showResult() 132 { 133 return $this->judge(); 134 } 135} 136 137if (!empty($_POST)) { 138 $me = new Me($_POST['last_name'], $_POST['first_name'], $_POST['choice'], $_POST['choice']); 139 $enemy = new Enemy(); 140 echo $me->getName().'は'.$me->getChoice().'を出しました。'; 141 echo '<br>'; 142 echo '相手は'.$enemy->getChoice().'を出しました。'; 143 echo '<br>'; 144 $battle = new Battle($me, $enemy); 145 echo '勝敗は'.$battle->showResult().'です。'; 146 if ($battle->showResult() === '勝ち') { 147 echo '<br>'; 148 echo $battle->countVictories().'回目の勝利です。'; 149 } 150} 151 152?> 153<!DOCTYPE html> 154<html lang="ja"> 155<head> 156<meta charset="utf-8"> 157<title>デバック練習</title> 158</head> 159<body> 160 <section> 161 <form action='./debug03.php' method="post"> 162 <label></label> 163 <input type="text" name="last_name" value="<?php echo '山田' ?>" /> 164 <label></label> 165 <input type="text" name="first_name" value="<?php echo '太郎' ?>" /> 166 <select name='choice'> 167 <option value=0 >--</option> 168 <option value=1 >グー</option> 169 <option value=2 >チョキ</option> 170 <option value=3 >パー</option> 171 </select> 172 <input type="submit" value="送信する"/> 173 </form> 174 </section> 175</body> 176</html>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

先頭で

PHP

1session_start(); 2if (!isset($_SESSION['result'])) { 3 $_SESSION['result'] = 0; 4}

としておけば良いのではないでしょうか。

投稿2022/07/01 11:20

itagagaki

総合スコア8402

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

BeatStar

2022/07/02 02:10

それ、私も引っかかったことありますね…
guest

0

$_SESSION['result'] += 1;

の直前の行に

PHP

1 $_SESSION['result']=$_SESSION['result']??0;

投稿2022/07/01 12:22

yambejp

総合スコア114839

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問