Array to unordered list

I am trying to match some array values ​​in an unordered list ().

<?php $files = scandir($dir); //remove "." and ".." print_r($files); ?> <ul> <?php foreach($files as $file): ?> <li><?= $file ?></li> <?php endforeach; ?> </ul> 

It iterates over the array correctly, as it gives bullets for the <li> elements. However, no filament is visible next to these bullets. Also, when I print the_r array, there are values.

The result is as follows with the correct number of bullets, but there is no text next to them:

.
.
.

What am I doing wrong here? Thanks in advance.

+6
source share
5 answers

I'm not sure about this, but it might work:

 <?php $files = scandir($dir); //remove "." and ".." print_r($files); ?> <ul> <?php foreach($files as $file) { ?> <li><?php echo $file; ?></li> <?php } ?> </ul> 

What would I use, and if that doesn't work, perhaps because your $dir variable contains nothing (has an error). One of the reasons why your source code may not have worked because I don’t think the <? ?> tags are <? ?> <? ?> compatible on each server. Also, from what I know in php there is no thing <?=$var ?> . I thought it exists only in ASP, etc.

EDIT: In response to your question about the inferiority of curly braces, they are a generally accepted standard in PHP. It could be different in C / C ++ / C # Family, I don't know.

+4
source
 function array2ul($array) { $out = "<ul>"; foreach($array as $key => $elem){ if(!is_array($elem)){ $out .= "<li><span>" . $key . ": " . $elem . "</span></li>"; } else { $out .= "<li><span>" . $key . "</span>" . array2ul($elem) . "</li>"; } } $out .= "</ul>"; return $out; } 
+6
source
 <?php foreach($files as $file): ?> <li><?php echo $file ?></li> <?php endforeach; ?> 

OR

 <?php foreach($files as $file): ?> <li><? echo $file ?></li> <?php endforeach; ?> 
+5
source

replace <?= $file ?> with <?php echo $file ?> or enable sorting tags by php.ini; It seems that your server is not turned on.

+3
source

Used today:

 if ( !empty($files) ) echo '<li>' . implode('<li>', $files); 
+1
source

Source: https://habr.com/ru/post/913185/


All Articles