当我运行我的老虎机,赢取累积奖金,刷新页面或点击按钮时,它说:
注意:未定义的索引' gokken'
但是当再次刷新时,错误消失,直到我获得新的累积奖金 为什么会这样?
<?php
session_start();
//session_destroy();
class Player
{
public $name;
public $age;
function __construct()
{
$this->name = 'Harm';
$this->age = 18;
}
public function setName($name)
{
return $this->name = $name;
}
public function setAge($age)
{
return $this->age = $age;
}
public function getAge($age)
{
if ($age < 18)
{
return "U mag niet spelen! <br/>";
} else
{
return 'Welkom ' . $this->name . ', u bent 18 jaar of ouder! <br/><br/>';
}
}
}
$player = new Player();
echo $player->getAge($player->age);
class Gokautomaat
{
public function Automaat()
{
$r1 = rand(0, 1);
$r2 = rand(0, 1);
$r3 = rand(0, 1);
$inzet = 100;
echo $r1 . $r2 . $r3 . '<br/><br/>';
if ($r1 == $r2 && $r3 == $r2)
{
echo 'U heeft de jackpot gewonnen!!<br><br> + €5000 <br/><br/>';
$_SESSION["gokken"] = $_SESSION["gokken"] + 5000;
echo 'Cash: $' . $_SESSION["gokken"];
session_destroy();
} else
{
//$_SESSION["gokken"] = $_SESSION["gokken"];
echo "U heeft niks gewonnen. <br/><br/> Bedrag - €$inzet" . '<br/><br/>';
$_SESSION["gokken"] = $_SESSION["gokken"] - $inzet;
echo 'Cash: €' . $_SESSION["gokken"];
}
}
public function Gokken()
{
if ($_GET["play"])
{
if (!isset($_SESSION["gokken"]))
{
$_SESSION["gokken"] = 1000;
}
echo '<form action=' . $_SERVER['PHP_SELF'] . ' method=get>';
echo '<input type=hidden name=play value=gokken>
<input type=submit value="Gok!"></form>';
} else
{
echo '<form action=' . $_SERVER['PHP_SELF'] . ' method=get>';
echo '<input type=hidden name=play value=gokken>
<input type=submit value="Gok!"></form>';
}
exit();
}
}
$gokautomaat = new Gokautomaat;
echo $gokautomaat->Automaat();
echo $gokautomaat->Gokken();
?>
答案 0 :(得分:0)
这是因为当你第一次启动$_SESSION["gokken"]
时没有设置它。正如您所看到的那样,有些行试图读取未设置的值:
$_SESSION["gokken"] = $_SESSION["gokken"] + 5000;
该值在Gokken()中初始化,但在Automaat()之后执行:
echo $gokautomaat->Automaat();
echo $gokautomaat->Gokken();
由于这是一个会话变量,它会在下次保持设置状态。 你可以解决这个问题。 例如,确保首先执行初始化。
或者您可以关闭通知:
error_reporting(E_ALL & ~E_NOTICE);
或者您可以使用“@”运算符隐藏特定行的任何错误:
@$_SESSION["gokken"] = $_SESSION["gokken"] + 5000;