I would like to receive input from more experience coders regarding the potential improvements this piece of code might have. I am new to PHP development and I am sure this code looks horrible behind the eyes of someone more competent. It works, but I think there's plenty of room for improvement.
I have an images
folder with several sub-directories, and inside them, different images are stored. The goal of this PHP snippet is to output HTML code in this format for each folder and files in my images
folder:
<section id='examplefolder' class='gallery'> <div id='categoryimg'> <p>examplefolder</p> </div> <div> <p>examplefilename</p> <img src='images/otros/examplefolder/examplefilename.jpg' alt='examplefilename'> </div> <div> <p>examplefilename2</p> <img src='images/otros/examplefolder/examplefilename2.jpg' alt='examplefilename'> </div> <!-- The snippet gets the rest of the files and places them under the folder name--> </section>
Here's said snippet:
<?php $folders = glob('images/otros/*', GLOB_ONLYDIR); foreach($folders as $folder) { echo "<section id='".basename($folder)."' class='gallery'><div id='categoryimg'><p>".basename($folder)."</p></div><!-- SECTION -->".PHP_EOL; $files = glob("images/otros/".basename($folder)."/*.{jpg}", GLOB_BRACE); foreach($files as $file) { echo "<div>".PHP_EOL." <p>".basename($file, ".jpg")."</p>".PHP_EOL." <img src='$file' alt='".basename($file, ".jpg")."'>".PHP_EOL."</div>".PHP_EOL; } echo "</section>"; } ?>
In particular, this one gets all folders with images inside images/otros
.
- Would you please offer any tips regarding best practices, or how to make my code look more readable?
- Have I used any deprecated functions?
- Is
PHP_EOL
the best way to add a newline character in order to make the output HTML look better?