我的
部分有以下内容<?php
$rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
$color = '#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];
?>
我的
中有以下内容<div class="featured-details" style="background-color: <?php echo $color; ?>;">
当我的页面加载时,CSS 显示如下:
<div style="background-color: ;" class="featured-details">
即似乎没有颜色生成。
有人能帮忙吗?
最佳答案
可能是你头脑中的代码片段与你 body 中的代码在不同的范围内执行,这就是为什么 $color
为空
如果 $color
在函数中,请尝试将其设为全局变量。
您需要添加 global $color;
,然后再将它设置在头部和 body 的某个位置。
关于php - echo 随机颜色不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13483125/