php PHP函数每次将变量增加1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10743807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:55:41  来源:igfitidea点击:

PHP function to increment variable by 1 each time

phpfunctionauto-incrementdecision-treeexpert-system

提问by Harry12345

I have started writing a PHP script for a game about creatures, there are 4 yes/no questions and what I am trying to do is write a function that will display 2 buttons that say yes and no and give then different names each time I run the function, for example yes1 and no1, then the next time the function is run the names of the buttons will be yes2 and no2.

我已经开始为一个关于生物的游戏编写 PHP 脚本,有 4 个是/否问题,我想要做的是编写一个函数,该函数将显示 2 个按钮,分别表示是和否,并在每次运行时给出不同的名称函数,例如 yes1 和 no1,那么下次运行该函数时,按钮的名称将是 yes2 和 no2。

I have attempted to do this already but it is not working correctly, below is the code I have done so far, any help would be much appreciated.

我已经尝试过这样做,但它无法正常工作,以下是我到目前为止所做的代码,任何帮助将不胜感激。

<?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>

回答by ShaaD

To do this you need to maintain state between requests.

为此,您需要在请求之间维护状态。

 function repeat() {
      $number = $_SESSION['number'];
      $number++;
      echo "<form method ='post' action='Creatures.php'>
     <input type='submit' name='answer' value='Yes' />
     <input type='submit' name='answer' value='No' />
     </form>";
     $_SESSION['number'] = $number;
 }

switch

转变

   if($_POST['answer']=='Yes') {
      switch($_SESSION['number']) {
            case 1:
            break;
            case 2:
            break;
      }
   } else {
      switch($_SESSION['number']) {
            case 1:
            break;
            case 2:
            break;
      }
   }

回答by Anthony Ogundipe

I am not in agreement with the accepted answer: Here is an easier way to go about it without session:

我不同意接受的答案:这是一种更简单的方法,无需会话:

function repeat() {
      static $number = 0;
      $number++;
      echo "<form method ='post' action='Creatures.php'>
     <input type='submit' name='answer' value='Yes' />
     <input type='submit' name='answer' value='No' />
     </form>";
 }

回答by Ruben Nagoga

You have to store 'number' in session because it will loose his value every time page submitted.

您必须在会话中存储“数字”,因为每次提交页面时它都会丢失他的值。