Output CSS class based on day-of-the-week match The goal is to loop through an array of days-of-the-week (array values are stored as strings, eg: 'Mon, Tues, Weds', etc) and output some HTML when today's day is encountered.

The file is a CMS's .tpl file, so the PHP sits directly alongside the HTML.

My current, working code is: 

&lt;?php
 $dayofweek = date('w');

 foreach ($dataarray['openingDays'] as $day_key =&gt; $opening_day):
   $i++;
   //if $i is equal to 7 make it 0 to match array
   $i = ($i == 7 ? 0 : $i);
   //If today's date matches $i
   if ($dayofweek == $i):
?&gt;
&lt;div class="todays-date"&gt;
&lt;?php else: ?&gt;
&lt;div class="not-todays-date"&gt;
&lt;?php
  endif;
  print $opening_day;
?&gt;
&lt;/div&gt;
&lt;?php endforeach; ?&gt;


This is one of the first times I've used ternary and would like to know if it is done correctly. I am also concerned about not initializing $i and would like to know if that is acceptable.
I would suggest you familiarize yourself with the DateTime class and it's related classes including DateInterval and DatePeriod as these are your most powerful tools for working with date/times, intervals of time, and iterating over intervals of time.

You should have no reason to build an array for days for the week or anything like that.

Let me give an example for how you might approach this problem using these classes.

// current DateTime
$now = new DateTime();
// create a DateTime representing the beginning of the week
$monday = clone $now;
$monday-&gt;setTime(0,0,0)
       -&gt;modify('Monday this week');
// create a DateInterval to represent a one day interval
$dayInterval = new DateInterval('P1D');
// create a DatePeriod to represent a full week
$weekPeriod = new DatePeriod($monday, $dayInterval, 7);
// iterate the DatePeriod
foreach($weekPeriod as $day) {
    if($day-&gt;format('D') === $now-&gt;format('D')) {
        // today matches this day of week
    }
}


Beyond that, I would suggest being cognizant of your styling.  Right now:
- your indentations are inconsistent
- your variable naming style in inconsistent between using snake_case, camelCase (like in $dataarray keys), and no way to differentiate words in your variable name at all (i.e.$dayofweek, $dataarray).  You should pick either snake_case or camelCase to make your code more readable and stick with whichever style you choose.  I would actually recommend camelCase as PHP, as a language, is moving more in that direction with regard to industry styling standards.