【发布时间】:2012-05-31 09:50:24
【问题描述】:
我已经开始为一个关于生物的游戏编写一个 PHP 脚本,有 4 个是/否问题,而我正在尝试编写一个函数,该函数将显示 2 个表示是和否的按钮,然后分别给出不同的名称当我运行该函数时,例如 yes1 和 no1,那么下次运行该函数时,按钮的名称将是 yes2 和 no2。
我已经尝试过这样做,但它不能正常工作,下面是我到目前为止所做的代码,非常感谢任何帮助。
<?php
session_set_cookie_params(2592000);
session_start();
?>
<html>
<head>
<title>Creature Guessing Game</title>
</head>
<body>
<h1>Creature Guessing Game</h1>
<p> Welcome to the creature guessing game! </p>
<p>Click the button below to start or restart the game </p>
<form method="post" action="Creatures.php">
<input type="submit" name="start" value="Start Game" />
</form>
<?php
$questions = array('Does the creature live on land?', 'Does it have wings?', 'Does it live near water?', 'Can it fly?');
function repeat()
{
$number = 0;
$number++;
echo "<form method ='post' action='Creatures.php'>
<input type='submit' name='yes'.$number value='Yes' />
<input type='submit' name='no'.$number value='No' />
</form>";
}
//If form not submitted, display form.
if (!isset($_POST['start'])){
?>
<?php
} //If form is submitted, process input
else{
switch($_POST)
{
case yes1:
echo $questions[0];
repeat();
break;
case no1:
echo $questions[1];
repeat();
break;
case yes2:
echo $questions[2];
repeat();
break;
case no2:
$questions[3];
repeat();
}
}
?>
</body>
</html>
【问题讨论】:
-
每次调用
repeat()时,您都将$number设置为0,然后将其递增。如果在设置$questions之后但在定义repeat()之前设置$number会怎样? -
我刚刚尝试过,但由于某种原因,当我尝试回显它时,$number 总是设置为 0,无论我调用函数 $number 多少次,它仍然是 0
标签: php function auto-increment decision-tree expert-system