Take the 2-minute tour ×

I am trying to create a class for a table td based on the string that is outputted within it.

<?php echo '<td class="'?>

    if ($day === 'monday') 
        echo 'mon';
    if ($day === 'tuesday') 
        echo 'tues';

<?php echo '">' . $day . '</td>'

However this results in the following:

<td class>monday</td>

I am trying to achieve this:

<td class="mon">monday</td>
<td class="tues">tuesday</td>

Any idea what I am doing wrong?

share|improve this question

closed as too localized by Corbin, hakre, chris, Mudassir, Lev Levitsky Nov 1 '12 at 9:26


Have you made sure that $day is actually either 'monday' or 'tuesday'? –  Ja͢ck Nov 1 '12 at 1:46
If you can control the css, you could change the classes to mon and tue, that way, the class value will be <?= substr($day, 0, 3) ?> and you won't need any if-else. –  xbonez Nov 1 '12 at 1:47
Holy scuffed new shoes! Can I please get an answer that at least looks maintainable? –  user166390 Nov 1 '12 at 1:49

5 Answers 5

I like xbonez and codingbiz answers, but if you access to the style sheet and can change it you could rename the classes so that they are called using the first 3 letters of the day and then just:

echo ('<td class="' . substr($day, 0, 3) . '">' . $day . '</td>');
share|improve this answer

I'd recommend deciding on the class beforehand to make the code more readable.

    $class = '';

    if ($day === 'monday') {
        $class = 'mon';
    } else if ($day === 'tuesday') {
        $class = 'tues';

<td class="<?= $class ?>"><?= $day ?></td>
share|improve this answer
<?= works only if shortags are enabled, correct? –  JakeParis Nov 1 '12 at 1:52
That is correct. If not enabled <?= should be replaced with <?php echo. –  xbonez Nov 1 '12 at 1:53

Separate the condition from the tag - makes it less error prone, maintainable, ...

   $shortday = '';
   if ($day === 'monday') 
      $shortday = 'mon';
   else if($day === 'tuesday')
      $shortday = 'tues';
   else if...

   echo "<td class=\"$shortday\">$day</td>";

share|improve this answer
Your final echo statement either needs string concat or single quotes around the class value. –  xbonez Nov 1 '12 at 1:46
Concatenation with what? I didn't see where? –  codingbiz Nov 1 '12 at 1:49
You've fixed it now. Your previous version was broken. –  xbonez Nov 1 '12 at 1:50
Alright. Thanks. –  codingbiz Nov 1 '12 at 1:59

A better way to do it would be to use a switch statement:

$day = 'tuesday';
switch ($day) {
    case 'monday':
        $shortDay = 'mon';
    case 'tuesday':
        $shortDay = 'tues';
        $shortDay = 'invalid';
echo '<td class="' . $shortDay . '">' . $day . '</td>';
share|improve this answer
Thanks for the reply. Sorry, yes, that was a typo, I've corrected that now but still getting the same problem. –  user1444027 Nov 1 '12 at 1:45
The elseif, although better, is not required here. –  Ja͢ck Nov 1 '12 at 1:45
@Jack what do you mean? It's not valid syntax if you don't have the else if –  sachleen Nov 1 '12 at 1:47
@sachleen His code just had two separate if statements; I don't see the invalid syntax. –  Ja͢ck Nov 1 '12 at 1:48
@Jack oh you're right, I must have deleted it when I was breaking it up into multiple lines. –  sachleen Nov 1 '12 at 1:50

Make an array with values for each of the <tr> lines in your code. Then loop through them. This will be the cleanest method. It's also a good practice for if you would have many more values of preknown items (like perhaps days of the month).

$days = array( 'mon' => 'monday',
               'tue' => 'tuesday',
               'wed' => 'wednesday'

foreach($days as $k => $v) {
    echo '<td class="' . $k . '">' . $v . '</td>';
share|improve this answer
