我正在尝试制作一个在每次刷新时更改图标的脚本。
<?php
$dir = "img/icons/";
$files = array();
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
$files[] = $file;
}
}
$icon = $files[rand(2,count($files)-1)];
echo "<LINK REL=\"shortcut icon\" HREF=\'img/icons/'.$icon."\">";
?>
此脚本从/img/icons 文件夹中获取每个图标,并随机选择一个。问题是,当我将其粘贴到 HTML 文件中时,页面顶部会出现奇怪的字符“; ?>。有人可以修复我的脚本吗?
最佳答案
<?php
$dir = "img/icons/";
$files = array();
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
$files[] = $file;
}
}
$icon = $files[rand(2,count($files)-1)];
echo '<LINK REL="shortcut icon" HREF="img/icons/'.$icon.'">';
?>
<强> Here 你可以看看在 HTML 中使用 PHP。您的服务器也应该为此进行正确配置。我总是推荐 .php
来处理 PHP 脚本。
希望这有帮助。
关于php - 每次刷新时随机出现图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10665570/