“;$ train [1] =“ 撒哈拉 </跨度> 沙漠”;$ train [2] =“俄罗斯”;$ train [3] =“切尔诺贝利”;$ train [4] =“美国”;$ train [5] =“朝鲜”;$ train [6] =“德国”;$ train [7] =“夏威夷”;?&GT;
&lt;!DOCTYPE html锟
你在寻找什么 array_rand() :
array_rand()
$random = array_rand($train);
这是代码中的一些问题
return
我会像这样改变php代码的最后一部分:
<?php if ($_POST['subButton']) { $cart = $_POST['cart']; $value = intval($cart); $roll = mt_rand(1, 8); // mt_rand() has more entropy than rand() if ($cart == 'show') { // show target locations (according the OP-source) for($x = 1; $x <= 8; $x++) { echo "<p> You could have ender up in... </p>"; echo "<h2> " . $train[$x-1] . "</h2>"; } } else if ($value > 0 && $value <= 8) { echo "<h2>"."Well, it looks like you're going to " . $train[$value-1] . "! Have fun! </h2>"; // idk why it needed, but move it here if ($value == $roll) { } } else if ($cart == 'any') { echo "<h2>"."Can't handle the pressure? You were selected to go to " . $train[$roll-1] . "! Have fun! </h2>"; } else { // $_POST['cart'] has something wrong } } ?> <!-- lines below was added to close html-tags --> </body> </html>
代替 rand() 使用 mt_rand() 因为 PHP文档 字面上说
rand()
mt_rand()
mt_rand锟“生成更好的随机值
关于你的PHP代码,你有很多错误。这是你的底层PHP代码应该如何看起来:
<?php if($_POST['subButton']) { $cart = $_POST['cart']; $roll = mt_rand(0,7); //0-7 because those are the values you inserted into //the $train[] array if($cart == 'show') { //another correction here, it has to be 0-7 for($x = 0; $x <= 7; $x++) { echo "<p> You could have ender up in... </p>"; echo "<h2> " . $train[$x] . "</h2>"; } } else if ($cart!='any') { "<h2>Well, it looks like you're going to " . $train[$cart] . "! Have fun! </h2>"; } else //took out return and placed an else { echo "<h2>Well, it looks like you're going to " . $train[$cart] . "! Have fun! </h2>"; } ?>