Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP随机数不工作_Php_Html_Image_Random_Input - Fatal编程技术网

PHP随机数不工作

PHP随机数不工作,php,html,image,random,input,Php,Html,Image,Random,Input,我想写一个php代码,显示一个随机图像,人们必须猜测它是什么。问题是我的php代码告诉我正确的城市是不正确的,我不明白为什么 <?php $random = rand(1, 3); $answer ; if ($random == 1) { $answer = "chicago" ;} elseif($random == 2) {$answer = "LA" ;} elseif($random == 3) {$answer = "NewYork" ;} else {echo "Answe

我想写一个php代码,显示一个随机图像,人们必须猜测它是什么。问题是我的php代码告诉我正确的城市是不正确的,我不明白为什么

<?php
$random = rand(1, 3);
$answer ;
if ($random == 1) { $answer = "chicago" ;} 
elseif($random == 2) {$answer = "LA" ;}
elseif($random == 3) {$answer = "NewYork" ;}
else {echo "Answer is None" ; } 
if (isset($_POST["choice"])) {
    $a = $_POST["choice"];
    if($a ==$answer){
    echo "<br> working <br>" ; 
    }else {echo "<br> Not Working <br>";}

}
 ?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>City Guess</title>
</head>
<body>
<center>
<img src="four/Image_<?php echo $random ;?>.jpg" width="960" height="639" alt=""/>

<form action="" method="POST">
chicago <input type= "radio" name="choice" value="chicago"  />
<br>
LA <input type= "radio" name="choice" value="LA" />
<br>
NewYork <input type= "radio" name="choice" value="NewYork" />
<br>
<input type ="submit" value="Submit" />
</form>

</center>
</body>
</html>

城市猜测
.jpg“width=“960”height=“639”alt=”“/>
芝加哥

洛杉矶
纽约

问题在于您的逻辑,您不存储用于显示图像的生成号码,而是在每次打开页面时生成一个新号码


您应该将生成的数字存储在(例如…)会话中以保留它并使用它进行比较。

问题在于您的逻辑,您不存储用于显示图像的生成数字,而是在每次打开页面时生成一个新数字


您应该将生成的数字存储在(例如…)会话中以保留它并使用它进行比较。

问题在于您的逻辑,您不存储用于显示图像的生成数字,而是在每次打开页面时生成一个新数字


您应该将生成的数字存储在(例如…)会话中以保留它并使用它进行比较。

问题在于您的逻辑,您不存储用于显示图像的生成数字,而是在每次打开页面时生成一个新数字


您应该将生成的号码存储在(例如…)保存它并使用它进行比较的会话。

每次加载页面时,您都会生成随机值。例如,当用户第一次访问它并获取表单时,您会生成
2
。当表单提交时,您会生成另一个值,而这一次是
3
。因此,即使用户正确地回答
2
,你说他们错了,因为你忘记了你最初问的问题

您需要以某种方式存储生成的值,并将其与响应进行比较,例如

$rand = rand(1,3);

<input type="hidden" name="correct_answer" value="$rand">


if ($_SERVER["REQUEST_METHOD"] == 'POST') {
   if ($_POST['correct_answer'] == $_POST['user_answer']) {
        echo 'correct';
   }
}
$rand=rand(1,3);
如果($\u服务器[“请求\u方法”]==“发布”){
如果($\u POST['correct\u answer']=$\u POST['user\u answer'])){
回声“正确”;
}
}

每次加载页面时,您都会生成随机值。例如,当用户第一次访问该页面并获取表单时,您会生成
2
。提交表单时,您会生成另一个值,这次是
3
。因此,即使用户正确回答了
2
,您也会说他们错了因为你忘记了你最初问的问题

您需要以某种方式存储生成的值,并将其与响应进行比较,例如

$rand = rand(1,3);

<input type="hidden" name="correct_answer" value="$rand">


if ($_SERVER["REQUEST_METHOD"] == 'POST') {
   if ($_POST['correct_answer'] == $_POST['user_answer']) {
        echo 'correct';
   }
}
$rand=rand(1,3);
如果($\u服务器[“请求\u方法”]==“发布”){
如果($\u POST['correct\u answer']=$\u POST['user\u answer'])){
回声“正确”;
}
}

每次加载页面时,您都会生成随机值。例如,当用户第一次访问该页面并获取表单时,您会生成
2
。提交表单时,您会生成另一个值,这次是
3
。因此,即使用户正确回答了
2
,您也会说他们错了因为你忘记了你最初问的问题

您需要以某种方式存储生成的值,并将其与响应进行比较,例如

$rand = rand(1,3);

<input type="hidden" name="correct_answer" value="$rand">


if ($_SERVER["REQUEST_METHOD"] == 'POST') {
   if ($_POST['correct_answer'] == $_POST['user_answer']) {
        echo 'correct';
   }
}
$rand=rand(1,3);
如果($\u服务器[“请求\u方法”]==“发布”){
如果($\u POST['correct\u answer']=$\u POST['user\u answer'])){
回声“正确”;
}
}

每次加载页面时,您都会生成随机值。例如,当用户第一次访问该页面并获取表单时,您会生成
2
。提交表单时,您会生成另一个值,这次是
3
。因此,即使用户正确回答了
2
,您也会说他们错了因为你忘记了你最初问的问题

您需要以某种方式存储生成的值,并将其与响应进行比较,例如

$rand = rand(1,3);

<input type="hidden" name="correct_answer" value="$rand">


if ($_SERVER["REQUEST_METHOD"] == 'POST') {
   if ($_POST['correct_answer'] == $_POST['user_answer']) {
        echo 'correct';
   }
}
$rand=rand(1,3);
如果($\u服务器[“请求\u方法”]==“发布”){
如果($\u POST['correct\u answer']=$\u POST['user\u answer'])){
回声“正确”;
}
}
试试看:

$random = floor(rand(1, 4));
我希望它有帮助。

试试看:

$random = floor(rand(1, 4));
我希望它有帮助。

试试看:

$random = floor(rand(1, 4));
我希望它有帮助。

试试看:

$random = floor(rand(1, 4));

我希望它能有所帮助。

使用
value=”“
在表单中添加以下隐藏字段,它将与您当前的代码一起工作

<input type="hidden" name="correct_answer" value="<?php echo $answer ?>">
记住,当你测试这个时,你有一个成功的机会


编辑 以下是我使用的代码:

<?php
$random = rand(1, 3);
$answer;
if ($random == 1) { $answer = "chicago" ;} 
elseif($random == 2) {$answer = "LA" ;}
elseif($random == 3) {$answer = "NewYork" ;}
else {echo "Answer is None" ; } 
if (isset($_POST["choice"])) {
    $a = $_POST["choice"];
    if($a ==$answer){
    echo "<br> working <br>" ; 
    }else {echo "<br> Not Working <br>";}
}
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>City Guess</title>
</head>
<body>
<center>

<img src="four/Image_<?php echo $random ;?>.jpg" width="960" height="639" alt=""/>

<form action="" method="POST">

<input type="hidden" value="<?php echo $answer ?>">

chicago <input type= "radio" name="choice" value="chicago"  />
<br>
LA <input type= "radio" name="choice" value="LA" />
<br>
NewYork <input type= "radio" name="choice" value="NewYork" />
<br>
<input type ="submit" value="Submit" />
</form>

</center>
</body>
</html>

城市猜测
.jpg“width=“960”height=“639”alt=”“/>

使用
value=”“
在表单中添加以下隐藏字段,它将与当前代码一起使用

<input type="hidden" name="correct_answer" value="<?php echo $answer ?>">
记住,当你测试这个时,你有一个成功的机会


编辑 以下是我使用的代码:

<?php
$random = rand(1, 3);
$answer;
if ($random == 1) { $answer = "chicago" ;} 
elseif($random == 2) {$answer = "LA" ;}
elseif($random == 3) {$answer = "NewYork" ;}
else {echo "Answer is None" ; } 
if (isset($_POST["choice"])) {
    $a = $_POST["choice"];
    if($a ==$answer){
    echo "<br> working <br>" ; 
    }else {echo "<br> Not Working <br>";}
}
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>City Guess</title>
</head>
<body>
<center>

<img src="four/Image_<?php echo $random ;?>.jpg" width="960" height="639" alt=""/>

<form action="" method="POST">

<input type="hidden" value="<?php echo $answer ?>">

chicago <input type= "radio" name="choice" value="chicago"  />
<br>
LA <input type= "radio" name="choice" value="LA" />
<br>
NewYork <input type= "radio" name="choice" value="NewYork" />
<br>
<input type ="submit" value="Submit" />
</form>

</center>
</body>
</html>

城市猜测
.jpg“width=“960”height=“639”alt=”“/>

使用
value=”“
在表单中添加以下隐藏字段,它将与当前代码一起使用

<input type="hidden" name="correct_answer" value="<?php echo $answer ?>">
记住,当你测试这个时,你有一个成功的机会


编辑 以下是我使用的代码:

<?php
$random = rand(1, 3);
$answer;
if ($random == 1) { $answer = "chicago" ;} 
elseif($random == 2) {$answer = "LA" ;}
elseif($random == 3) {$answer = "NewYork" ;}
else {echo "Answer is None" ; } 
if (isset($_POST["choice"])) {
    $a = $_POST["choice"];
    if($a ==$answer){
    echo "<br> working <br>" ; 
    }else {echo "<br> Not Working <br>";}
}
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>City Guess</title>
</head>
<body>
<center>

<img src="four/Image_<?php echo $random ;?>.jpg" width="960" height="639" alt=""/>

<form action="" method="POST">

<input type="hidden" value="<?php echo $answer ?>">

chicago <input type= "radio" name="choice" value="chicago"  />
<br>
LA <input type= "radio" name="choice" value="LA" />
<br>
NewYork <input type= "radio" name="choice" value="NewYork" />
<br>
<input type ="submit" value="Submit" />
</form>

</center>
</body>
</html>

城市猜测
.jpg“width=“960”height=“639”alt=”“/>

使用
value=”“
在表单中添加以下隐藏字段,它将与当前代码一起使用

<input type="hidden" name="correct_answer" value="<?php echo $answer ?>">
记住,当你测试这个时,你有一个成功的机会


编辑 以下是我使用的代码:

<?php
$random = rand(1, 3);
$answer;
if ($random == 1) { $answer = "chicago" ;} 
elseif($random == 2) {$answer = "LA" ;}
elseif($random == 3) {$answer = "NewYork" ;}
else {echo "Answer is None" ; } 
if (isset($_POST["choice"])) {
    $a = $_POST["choice"];
    if($a ==$answer){
    echo "<br> working <br>" ; 
    }else {echo "<br> Not Working <br>";}
}
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>City Guess</title>
</head>
<body>
<center>

<img src="four/Image_<?php echo $random ;?>.jpg" width="960" height="639" alt=""/>

<form action="" method="POST">

<input type="hidden" value="<?php echo $answer ?>">

chicago <input type= "radio" name="choice" value="chicago"  />
<br>
LA <input type= "radio" name="choice" value="LA" />
<br>
NewYork <input type= "radio" name="choice" value="NewYork" />
<br>
<input type ="submit" value="Submit" />
</form>

</center>
</body>
</html>

城市猜测
.jpg“width=“960”height=“639”alt=”“/>

这是因为你的$random在每次页面加载时都会重置,所以当表单发布时,第一次加载会选择1,它会选择3。你需要在表单中存储第一次加载的$random,并将其作为“正确”传递“生成一个隐藏字段,然后将您提交的答案与随机答案进行比较。此函数将返回介于1和3之间的浮点值,而不是1、2或3。你需要使用类似floor($random)的东西。random将生成一个新的页面加载随机数。胡伊