PHP snippet to create image gallery from folders with jpgs 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:

&lt;section id='examplefolder' class='gallery'&gt;
    &lt;div id='categoryimg'&gt;
        &lt;p&gt;examplefolder&lt;/p&gt;
    &lt;/div&gt;
    &lt;div&gt;
        &lt;p&gt;examplefilename&lt;/p&gt;
        &lt;img src='images/otros/examplefolder/examplefilename.jpg' alt='examplefilename'&gt;
    &lt;/div&gt;
    &lt;div&gt;
        &lt;p&gt;examplefilename2&lt;/p&gt;
        &lt;img src='images/otros/examplefolder/examplefilename2.jpg' alt='examplefilename'&gt;
    &lt;/div&gt;
    &lt;!-- The snippet gets the rest of the files and places them under the folder name--&gt;
&lt;/section&gt;


Here's said snippet:

&lt;?php
    $folders = glob('images/otros/*', GLOB_ONLYDIR);
    foreach($folders as $folder) {
        echo "&lt;section id='".basename($folder)."' class='gallery'&gt;&lt;div id='categoryimg'&gt;&lt;p&gt;".basename($folder)."&lt;/p&gt;&lt;/div&gt;&lt;!-- SECTION --&gt;".PHP_EOL;
            $files = glob("images/otros/".basename($folder)."/*.{jpg}", GLOB_BRACE);
            foreach($files as $file) {
                echo "&lt;div&gt;".PHP_EOL."    &lt;p&gt;".basename($file, ".jpg")."&lt;/p&gt;".PHP_EOL."    &lt;img src='$file' alt='".basename($file, ".jpg")."'&gt;".PHP_EOL."&lt;/div&gt;".PHP_EOL;
            }
        echo "&lt;/section&gt;";
    }
?&gt;


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?
Is PHP_EOL the best way to add a newline character in order to make the output HTML look better?


No, I don't think so. If you want your HTML nice on output, why not have it nice in the source code as well?

So instead of this:

echo "&lt;div&gt;".PHP_EOL."    &lt;p&gt;".basename($file, ".jpg")."&lt;/p&gt;".PHP_EOL."    &lt;img src='$file' alt='".basename($file, ".jpg")."'&gt;".PHP_EOL."&lt;/div&gt;".PHP_EOL;


have this:

echo "&lt;div&gt;
        &lt;p&gt;".basename($file, ".jpg")."&lt;/p&gt;
        &lt;img src='$file' alt='".basename($file, ".jpg")."'&gt;
      &lt;/div&gt;;



  Have I used any deprecated functions? 


I don't think so. But generally, you can see that yourself, when you enable warnings in development (don't forget to turn them of in production though).


  Would you please offer any tips regarding best practices, or how to make my code look more readable?



You could extract code blocks to functions
You could use more whitespace (eg around .)
You could extract duplicate function calls to well-named variables
have constants such as fixed directory names in as few places as possibly, to make changing it easier


This would give you something like this. It's not perfect, but hopefully a good start:

echo formatHTMLforDirectory('images/otros/');

/**
TODO PHPDoc comment
*/
function formatHTMLforDirectory($basedirectory) {   
    $directories = glob("$basedirectory/*", GLOB_ONLYDIR); 
    $html = "";
    foreach($directories as $directory) {
        $directoryName = basename($directory);
        $html .= "&lt;section id='$directoryName' class='gallery'&gt;
                    &lt;div id='categoryimg'&gt;
                        &lt;p&gt;$directoryName&lt;/p&gt;
                    &lt;/div&gt;
                    &lt;!-- SECTION --&gt;";
        $files = glob("$basedirectory/$directoryName/*.{jpg}", GLOB_BRACE);
        $html .= formatHTMLforFiles($files);
        $html .= "&lt;/section&gt;";
    }
    return $hmtl;
}

/**
TODO PHPDoc comment
*/
function formatHTMLforFiles(array $files) {
    $html = "";
    foreach($files as $file) {
        $filename = basename($file, ".jpg");
            $html .+ "&lt;div&gt;
                        &lt;p&gt;$filename&lt;/p&gt;
                        &lt;img src='$file' alt='$filename'&gt;
                      &lt;/div&gt;";
    }
    return $hmtl;
}


Misc


I always get nervous when data from the outside is echoed unencoded, as it can lead to XSS. If you allow users to upload image files, you are already vulnerable. But even if not, you may allow this in the future, which would make you vulnerable then.
You might want to think about using a templating engine such as twig. Mixing HTML and PHP often leads to hard to read code.