-1

我试图使用此代码结合使用 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>
4

2 回答 2

5

您的if子句使用 assignment=而不是==.

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

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

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

另外,对于这么多 if else 情况,建议改用用switch例。

switch($z) {
  case 1:
    // ...
    break;
  case 2:
    // ...
    break;
}
于 2013-11-12T03:19:51.020 回答
1

好吧,我会简化所有这些并避免代码重复。

<?php
    $letter = chr(ord("a") + mt_rand(0, 9));
    echo "animation:".$letter." 5s linear 1s infinite alternate; ";
    echo "-webkit-animation:".$letter." 5s linear 1s infinite alternate; ";
    echo "-mox-animation:".$letter." 5s linear 1s infinite alternate; ";
    echo "-o-animation:".$letter." 5s linear 1s infinite alternate;";
于 2013-11-12T03:30:47.183 回答