每个字母都有不同颜色的字符串,HTML PHP

时间:2017-06-06 15:35:50

标签: php html arrays for-loop

我需要使用HTML和PHP打印不同颜色(不是随机颜色)的字符串的每个字符。它几乎可以工作,但阵列的第一个字母是黑色的。你知道为什么吗?

<html>
    <?php
        $myString = ["s","t","r","i","n","g"];
        $myColors = ["blue","green","yellow","brown","gray","pink"];

        for ($i = 0; $i < count($myString); $i++) { 
            echo "$myString[$i] <span style='color:$myColors[$i]'</span>";
        }
    ?>
</html>

4 个答案:

答案 0 :(得分:2)

好像你没有正确关闭你的span标签,把你的字符串放在你的跨度中会有助于它的着色。

<?php
$myString = ["s","t","r","i","n","g"];
$myColors = ["blue","green","yellow","brown","gray","pink"];

for ($i = 0; $i < count($myString); $i++) { 
   echo "<span style='color:$myColors[$i]'>$myString[$i]</span>";
}

答案 1 :(得分:0)

角色不在彩色范围内且你错过>

echo "<span style='color:$myColors[$i]'>$myString[$i]</span>";

答案 2 :(得分:0)

我认为您的HTML已损坏。您需要在$myString[$i]元素中加入<span>并正确关闭它。

$myString = ["s","t","r","i","n","g"];
$myColors = ["blue","green","yellow","brown","gray","pink"];

for ($i = 0; $i < count($myString); $i++) { 
   echo "<span style='color:$myColors[$i]'>$myString[$i]</span>";
}

答案 3 :(得分:0)

<html>
<?php
$myString = ["s","t","r","i","n","g"];
$myColors = ["blue","green","yellow","brown","gray","pink"];

for ($i = 0; $i < count($myString); $i++) { 

   echo "<span style='color:$myColors[$i]'> $myString[$i] </span>";

}
?>
</html>