English 中文(简体)
PHP 函数为递增变量,每次增加1
原标题:PHP function to increment variable by 1 each time

我已经开始为关于生物的游戏撰写 PHP 脚本, 有 4 是/ no 个问题, 我试图做的是写一个函数, 它将显示两个按钮, 显示是和否, 然后每次运行函数时给出不同的名称, 比如是 是 1 和 No 1, 然后下次运行函数时, 按钮的名称将是是 是 2 和 no 2 。

我已尝试这样做,但工作不正确,下面是我至今所遵循的守则,任何帮助都将非常感激。

<?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>
最佳回答

要做到这一点,您需要保持请求之间的状态。

 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;
 }

开关

   if($_POST[ answer ]== Yes ) {
      开关($_SESSION[ number ]) {
            case 1:
            break;
            case 2:
            break;
      }
   } else {
      开关($_SESSION[ number ]) {
            case 1:
            break;
            case 2:
            break;
      }
   }
问题回答

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>";
 }

您必须将数字保存在会话或 cookie 中, 因为它会松开提交( 重新加载) 的每页时间的值 。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签