大佬教程收集整理的这篇文章主要介绍了php – 在数组中使用随机变量,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前正在从事在线计算机科学课程,但是我对如何做一小部分非常困惑.我们需要为此活动使用数组,其中用户有多个选择,每个不同的选择具有不同/唯一的文本输出.一切正常,除了我需要一个选择随机选择的选项,但是我对如何制作它有点困惑.您可以从我的代码中看到选项1-8.我希望它随机选择其中一个选项.
这是我的代码:
<?php $Train[0] = "Canada"; $Train[1] = "Sahara Desert"; $Train[2] = "Russia"; $Train[3] = "Chernobyl"; $Train[4] = "United States"; $Train[5] = "North Korea"; $Train[6] = "GeRMANy"; $Train[7] = "Hawaii"; ?> <!DOCTYPE html> <html> <head> Took out everything here,it's not important. </head> <body> <center> <h1>Vacation Time!</h1> <h4>You and your family just won the lottery! You all want to go on vacation,but nobody can agree where to go. Inside each Train cart has a card with a LOCATIOn written on it. whatever you find is where you're going! </h4> <form name="form1" action="activity-2-7-arrays-a.php" method="post"> <label>Which cart on the Train do you want to choose?</label> <br> <SELEct name="cart" required> <option value="1">First Cart</option> <option value="2">Second Cart</option> <option value="3">Third Cart</option> <option value="4">Fourth Cart</option> <option value="5">Fifth Cart</option> <option value="6">Sixth Cart</option> <option value="7">Seventh Cart</option> <option value="8">Eight Cart</option> <option value="show">Show all options</option> <option value="any">Choose Randomly</option> <br> </SELEct><br/> <input type="submit" name="subButton" class="subButton" value="Go!"/><br/> </form> <h1><u>Final Results</u></h1> <?php if($_POST['subButton']) { $cart = $_POST['cart']; $roll = rand(1,9); if($cart == show) { for($x = 1; $x <= 9; $x++) { echo "<p> You could have ender up in... </p>"; echo "<h2> " . $Train[$x] . "</h2>"; } return; } echo "<h2>"."Well,it looks like you're going to " . $Train[$cart] . "! Have fun! </h2>"; } return; if ($cart == $roll) { } echo "<h2>"."Can't handle the pressure? you were SELEcted to go to " . $Train[$roll] . "! Have fun! </h2>"; ?>
我也确定它有点乱.希望你明白我的意思.如果你能够向我解释这个非常有用的答案.谢谢 :)
<?php if($_POST['subButton']) { $cart = $_POST['cart']; $roll = rand(1,9);
在检查用户是否选择“随机选择”以及为什么生成1到9之间的随机值之前,您正在生成随机值? $Train数组以索引0开头,以索引7结束.
if($cart == show) {
字符串需要引用.
for($x = 1; $x <= 9; $x++) {
再次将$x从1循环到9是没有意义的,因为你的数组索引.
echo "<p> You could have ender up in... </p>"; echo "<h2> " . $Train[$x] . "</h2>";
由于$Train中的最后一个指数为7,因此当$x达到8时将失败.
} return; } echo "<h2>"."Well,it looks like you're going to " . $Train[$cart] . "! Have fun! </h2>"; } return;
因此,如果用户没有选择“显示所有选项”,则会向他显示他所选择的位置.如果用户选择“随机选择”,这将失败,因为$cart将具有值’any’且$Train [‘any’]不存在.
这是具有正确逻辑的代码.
<?php if($_POST['subButton']) { $cart = $_POST['cart']; if ($cart == 'any') {// check if user SELEcted 'Choose Randomly' $roll = rand(0,7); echo "<h2>"."Can't handle the pressure? you were SELEcted to go to " . $Train[$roll] . "! Have fun! </h2>"; } else { if ($cart == 'show') { // If user SELEcted 'Show all options' echo "<p> You could have ender up in... </p>"; for($x = 0; $x <= 7; $x++) { echo "<h2> " . $Train[$x] . "</h2>"; } } else { // User SELEcted cart so show him chosen LOCATIOn echo "<h2>"."Well,it looks like you're going to " . $Train[$cart] . "! Have fun! </h2>"; } } } ?>
以上是大佬教程为你收集整理的php – 在数组中使用随机变量全部内容,希望文章能够帮你解决php – 在数组中使用随机变量所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。