PHP选择一方死了有吗?
我的代码的 php 部分没有执行正确的操作 当用户提交他/她的号码时,例如,如果用户提交5,我希望rand函数随机输出5作为所选的号码;但是,我的代码有时有效,有时则无效。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
choose a die number
</title>
</head>
<center>
<body>
<h1>Choose a die number</h1>
<form method = "post" action="choosedie.php"/>
<!--If the user selects a number of sides the die is suppose to have
rand should go through and pick the number randomly, and echo out the
number that was entered by the user-->
Please choose how many sides a die should have from 1 through 6:
<br/>
<input type = "text" name = "roll"/>
<?php
//Roll a random dice from 1 through 6:
$roll = rand(1,6);
// Add random variable to do comparison on.
有没有办法让我在这里比较 rand,而不必创建另一个变量?
$random = rand(1,6);
// If $roll is equal to $random echo the output out to the user.
If ($roll == $random)
{
echo "<br/>Here is the random $roll with the appropriate maximum value $random\n";
如果用户选择 5,我希望它回显 5,但我得到不同的值?
}
?>
</body>
</center>
</html>
The php section of my code is not doing the correct operations
when the user submits his/her number, for example, if the user submits 5, I want the rand function to randomly output 5 as the number selected; however, my code sometimes works and at other times does not work.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
choose a die number
</title>
</head>
<center>
<body>
<h1>Choose a die number</h1>
<form method = "post" action="choosedie.php"/>
<!--If the user selects a number of sides the die is suppose to have
rand should go through and pick the number randomly, and echo out the
number that was entered by the user-->
Please choose how many sides a die should have from 1 through 6:
<br/>
<input type = "text" name = "roll"/>
<?php
//Roll a random dice from 1 through 6:
$roll = rand(1,6);
// Add random variable to do comparison on.
Is there a way for me to just compare rand here, without having to create another variable?
$random = rand(1,6);
// If $roll is equal to $random echo the output out to the user.
If ($roll == $random)
{
echo "<br/>Here is the random $roll with the appropriate maximum value $random\n";
If the user selects 5, I want this to echo out 5, but I am getting different values?
}
?>
</body>
</center>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于 rand(n,m) 返回一个整数值,您应该能够这样做:
Since
rand(n,m)
returns an integer value, you should be able to do thusly:我认为你在这里想做的是要求用户输入骰子有多少面,然后输出该值是多少?这个问题没有多大意义......但如果上面是您想要做的,那么这应该可行:
choosedie.php
这将允许用户输入的数量骰子有面,然后打印出滚动的内容以及面数。
I think what you're trying to do here is ask the user to input how many sides the die has and then output what the value is? The question doesn't make a lot of sense... but if the above is what you're trying to do, this should work:
choosedie.php
This will allow the user to input the number of sides a die has, and then print out what was rolled along with the number of sides.
我不确定你想做什么。每次加载此页面时,它都会选择两个随机数,$random 和 $roll,如果它们相等,则打印它们。
如果您想获取用户输入,则需要使用 $_POST。
另外,你不需要提交按钮吗?
I'm not sure what you're trying to do. Each time this page is loaded, it will pick two random numbers, $random and $roll, and if they are equal, print them.
If you want to get the users input, you'll need to use $_POST.
Also, don't you need a submit button?