php - rand() 只返回 1 作为答案

标签 php css

<分区>


关闭 9 年前

  • 这个问题是由于打字错误或无法再重现的问题引起的。虽然类似的问题可能是on-topic在这里,这个问题的解决方式不太可能帮助 future 的读者。
  • 要求提供代码的问题必须表现出对所解决问题的最低限度的理解。包括尝试过的解决方案、为什么它们不起作用,以及预期结果。另请参阅:Stack Overflow question checklist

我试图通过结合使用此代码的 php 和 css 在 10 个动画之间随机切换。它一切正常,除了 rand() 始终显示为“1”,无论我重新加载页面或打开和关闭选项卡多少次。我做错了什么?

<style type="text/css">
#a {width:200; height:200; background-color:black; color:red; font-size:30; position:relative; left:50; top:50;
<?php
$z=mt_rand(1,10);
if ($z="1")
{echo "animation:a 5s linear 1s infinite alternate; -webkit-animation:a 5s linear 1s infinite alternate; -mox-animation:a 5s linear 1s infinite alternate; -o-animation:a 5s linear 1s infinite alternate;";}
elseif($z="2")
{echo "animation:b 5s linear 1s infinite alternate; -webkit-animation:b 5s linear 1s infinite alternate; -mox-animation:b 5s linear 1s infinite alternate; -o-animation:b 5s linear 1s infinite alternate;";}
elseif($z="3")
{echo "animation:c 5s linear 1s infinite alternate; -webkit-animation:c 5s linear 1s infinite alternate; -mox-animation:c 5s linear 1s infinite alternate; -o-animation:c 5s linear 1s infinite alternate;";}
elseif($z="4")
{echo "animation:d 5s linear 1s infinite alternate; -webkit-animation:d 5s linear 1s infinite alternate; -mox-animation:d 5s linear 1s infinite alternate; -o-animation:d 5s linear 1s infinite alternate;";}
elseif($z="5")
{echo "animation:e 5s linear 1s infinite alternate; -webkit-animation:e 5s linear 1s infinite alternate; -mox-animation:e 5s linear 1s infinite alternate; -o-animation:e 5s linear 1s infinite alternate;";}
elseif($z="6")
{echo "animation:f 5s linear 1s infinite alternate; -webkit-animation:f 5s linear 1s infinite alternate; -mox-animation:f 5s linear 1s infinite alternate; -o-animation:f 5s linear 1s infinite alternate;";}
elseif($z="7")
{echo "animation:g 5s linear 1s infinite alternate; -webkit-animation:g 5s linear 1s infinite alternate; -mox-animation:g 5s linear 1s infinite alternate; -o-animation:g 5s linear 1s infinite alternate;";}
elseif($z="8")
{echo "animation:h 5s linear 1s infinite alternate; -webkit-animation:h 5s linear 1s infinite alternate; -mox-animation:h 5s linear 1s infinite alternate; -o-animation:h 5s linear 1s infinite alternate;";}
elseif($z="9")
{echo "animation:i 5s linear 1s infinite alternate; -webkit-animation:i 5s linear 1s infinite alternate; -mox-animation:i 5s linear 1s infinite alternate; -o-animation:i 5s linear 1s infinite alternate;";}
elseif($z="10")
{echo "animation:j 5s linear 1s infinite alternate; -webkit-animation:j 5s linear 1s infinite alternate; -mox-animation:j 5s linear 1s infinite alternate; -o-animation:j 5s linear 1s infinite alternate;";}
?>
}
</style>

最佳答案

您的 if 子句使用赋值 = 而不是 ==

另外,我猜你的 $z 是一个整数,对吧?无需用双引号引起来。

if($z === 1) {
  // ...
} elseif($z === 2) {
  // ...
}

旁注: === 也会检查其数据类型。

此外,对于很多 if else 情况,建议改用 switch 情况。

switch($z) {
  case 1:
    // ...
    break;
  case 2:
    // ...
    break;
}

关于php - rand() 只返回 1 作为答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19920248/

上一篇:html - 不使用 DIV 居中 IMG 标签?

下一篇:JavaScript html 编辑错误

相关文章:

php - 文件不存在或未找到

php date() 函数显示错误的时间

javascript - Ajax 请求未重定向到 URL

php - 从 apache 服务器中的 php 访问 shell 可执行文件

html - 将鼠标悬停在 <li> 上使其他 <ul> 消失

javascript - 如何使用css选择SVG线xy坐标?

javascript - 在 Bootstrap 中禁用输入。如何将其应用于不同的 TAG?

css - 最大化时,为什么在 "and"之后有一个换行符?

php - php中是否可以进行python丰富的比较?

HTML 固定位置表