Dropdown menu with submenu and heredocs I was recently going through and refactoring old code, and I eventually came across the code for my website's menu bar, so I started to refactor it.

I also recently was introduced to Heredocs, which I am very new to and not sure all the ins-and-outs yet.

Here is the refactored code for part of it (I am mostly interested in responses about this specific part of the menu, which is a dropdown menu that every logged in user gets that essentially has their name and then it drops down into some other options regarding themes):

&lt;?php

    $showAdminPanel = ((hasPerms($_SESSION['users_perms'], $_SESSION['role_perms'], 8)) ? "&lt;li&gt;&lt;a href='{$backwards}../admin/'&gt;Admin Panel&lt;/a&gt;&lt;/li&gt;":"");
    $devPerms = ((hasPerms($_SESSION['users_perms'], $_SESSION['role_perms'], 13)) ? "&lt;li&gt;
                                                                                        &lt;li&gt;&lt;a href='{$backwards}development/issues/index.php'&gt;Issues&lt;/a&gt;&lt;/li&gt;
                                                                                        &lt;li&gt;&lt;a href='{$backwards}development/requests/index.php'&gt;Requests&lt;/a&gt;&lt;/li&gt;
                                                                                        &lt;li&gt;&lt;a href='{$backwards}development/changes.php'&gt;Changelog&lt;/a&gt;&lt;/li&gt;
                                                                                      &lt;/li&gt;":"");

    $themes = "";
    foreach(scandir($assetsDir."themes/") as $file) {
        if((strcmp($file,".") != 0) &amp;&amp; (strcmp($file,"..") != 0)) {
            $themes .= '&lt;li&gt;&lt;a class="theme" href="#" rel="'.$themePath."/".$file.'"&gt;'.substr(ucfirst($file),0,-4).'&lt;/a&gt;&lt;/li&gt;';
        }
    }

    echo &lt;&lt;&lt;USERACCOUNTDROPDOWN
        &lt;li class="menu-item dropdown"&gt;
            &lt;a href="#" class="dropdown-toggle" data-toggle="dropdown"&gt;{$_SESSION["user"]["name"]}&lt;b class="caret"&gt;&lt;/b&gt;&lt;/a&gt;
            &lt;ul class="dropdown-menu"&gt;
                {$showAdminPanel}
                &lt;li id="nav" class="menu-item dropdown dropdown-submenu"&gt;
                    &lt;a href="#" class="dropdown-toggle" data-toggle="dropdown"&gt;Themes&lt;/a&gt;
                    &lt;ul class="dropdown-menu"&gt;
                        &lt;li&gt;&lt;a class="theme" href="#" rel="{$themePath}/cosmo.css"&gt;Default: Cosmo&lt;/a&gt;&lt;/li&gt;
                        &lt;li role="separator" class="divider"&gt;&lt;/li&gt;
                        {$themes}
                    &lt;/ul&gt;
                &lt;/li&gt;
                &lt;li id="nav" class="menu-item dropdown dropdown-submenu"&gt;
                    &lt;a href="#" class="dropdown-toggle" data-toggle="dropdown"&gt;Development&lt;/a&gt;
                    &lt;ul class="dropdown-menu"&gt;
                        {$devPerms}
                    &lt;/ul&gt;
                &lt;/li&gt;
                &lt;li role="seperator" class="divider"&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href="{$backwards}login/index.php"&gt;Logout&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
        &lt;/li&gt;
USERACCOUNTDROPDOWN;
?&gt;


The reason that I am more interested in this section of the code rather than the entire menu bar is that, this part of it has some permissions baked in using the $devPerms variable, basically if user has perm ID 13 it creates a string which has links to specific parts of the website, if user does not, the string is blank.

This code works, but I feel like there is a better way to accomplish what I am trying to do here that will make it look cleaner.

My question is this: How would you have written this code? I would love to improve my Heredocs understanding as I believe it will improve my programming in general.
Excessive perens in your ternary - they don't help with readability, scrap em...

$showAdminPanel = hasPerms($_SESSION['users_perms'], $_SESSION['role_perms'], 8) ? "&lt;li&gt;&lt;a href='{$backwards}../admin/'&gt;Admin Panel&lt;/a&gt;&lt;/li&gt;" : "";


For long ternary lines I sometimes like to put a line break and a tab before the ? and :.

$showAdminPanel = hasPerms($_SESSION['users_perms'], $_SESSION['role_perms'], 8) 
    ? "&lt;li&gt;&lt;a href='{$backwards}../admin/'&gt;Admin Panel&lt;/a&gt;&lt;/li&gt;" 
    : "";


The second ternary operator contains invalid HTML. &lt;li&gt; must be a child of &lt;ul&gt; or &lt;ol&gt;, not another &lt;li&gt;.

Using strcmp seems like overkill in the scandir loop. it would be faster to just use an == operator. Also, it's generally better practice to creatte and append to an array than to a string. Every time you add to taht string in that loop, the entire string has to be loaded into memory all over again. It's much quicker to use a buffer liek this...

$themes = array();
foreach(scandir($assetsDir."themes/") as $file) {
    if($file == "." || $file == "..") continue;
    $themes[] = '&lt;li&gt;&lt;a class="theme" href="#" rel="'.$themePath."/".$file.'"&gt;'.substr(ucfirst($file),0,-4).'&lt;/a&gt;&lt;/li&gt;';
}
// Turn the buffer into a string
$themes = implode('', $themes);


There is no reason for a heredoc here since you're at the end of the script. Just close the php tag and put the stuff after that (replacing the curlies with &lt;?php= and ;?&gt;), that way you don't have the PHP interpreter interpreting things it doesn't need to interpret.