数组到无序列表 [英] Array to an unordered list
问题描述
我正在尝试将一些数组值映射到一个无序 () 列表.
I'm trying to map some arrays values to an unordered () list.
<?php
$files = scandir($dir);
//remove "." and ".."
print_r($files);
?>
<ul>
<?php foreach($files as $file): ?>
<li><?= $file ?></li>
<?php endforeach; ?>
</ul>
它确实正确地遍历数组,因为它为 元素提供了项目符号.但是,在这些项目符号旁边看不到字符串输出.同样,当我打印_r 数组时,值就在那里.
It does iterate through the array correctly as it gives bullets for <li>
elements. However no string output is seen next to those bullets. Also when I print_r the array the values are there.
输出看起来像这样,项目符号数量正确,但旁边没有文字:
The output looks like this with the correct number of bullets but no text next to them:
.
.
.
.
.
.
我在这里做错了什么?提前致谢.
What am I doing wrong here? Thanks in advance.
推荐答案
我不确定这个,但这可能有用:
I'm not sure about this, but this might work:
<?php
$files = scandir($dir);
//remove "." and ".."
print_r($files);
?>
<ul>
<?php foreach($files as $file) { ?>
<li><?php echo $file; ?></li>
<?php } ?>
</ul>
这就是我要使用的,如果这不起作用,那可能是因为您的 $dir
变量不包含任何内容(有错误).您的原始代码可能不起作用的原因之一,因为我不认为 <??>
标签在每台服务器上都是兼容的.另外,据我所知,php 中没有 <?=$var ?>
的东西.我以为它只存在于ASP之类的.
That's what I would use, and if this doesn't work, it might be because your $dir
variable contains nothing (has an error). One reason why your original code might not have worked, because I don't think the <? ?>
tags are compatible on every server. Also, from what I know, there is no <?=$var ?>
thing in php. I thought it only exists in ASP and the like.
在回答您关于大括号的劣势的问题时,它们是 PHP 中普遍接受的标准.这在 C/C++/C# 家族中可能有所不同,我不知道.
In answer to your question about the inferiority of curly braces, they are the commonly accepted standard in PHP. This might be different in the C/C++/C# Family, I don't know.
这篇关于数组到无序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!