PHP随机数不起作用 [英] PHP random number not working

查看:95
本文介绍了PHP随机数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个显示随机图像的PHP代码,并且该人必须猜测它是什么。问题是我的PHP代码告诉我正确的城市是不正确的,我无法弄清楚为什么

I want to write a php code that displays a random image and the person has to guess what it is. The problem is my php code tells me that the right city is incorrect, i cant figure out why

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


推荐答案

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

The problem is your logic, you do not store the generated number that is used to show the image, instead you generate a new number every time you open the page.

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

You should store the generated number in (for example...) a session to preserve it and use that to compare.

这篇关于PHP随机数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆