-2

I want to make the text red if username == user_id but it gives me a syntax error, unexpected 'if' (T_IF). my code:

foreach ($dag as $taak) {
    echo '<li' if($_SESSION['user_id'] == $taak['username']){ echo 'style="color:red"';}'>'.$taak['taak_naam'].' - '.$taak['username'].'</li>';
}

anyone any idea?

5 Answers5

2

It should be something like this

echo '<li';

if($_SESSION['user_id'] == $taak['username']){ 

echo ' style="color:red"';

}

echo '>'.$taak['taak_naam'] . ' - '  .$taak['username'] . '</li>';
JYoThI
  • 11,977
  • 1
  • 11
  • 26
1

Don't mix decisions (if ...) with output. Prepare everything before echoing:

foreach ($dag as $taak) {
    // Don't add color by default
    $style = '';
    // Special color for special entries
    if ($_SESSION['user_id'] == $taak['username']) {
        $style = ' style="color: red;"';
    }

    // Make sure HTML special characters in $taak['username'] do not break the HTML
    $username  = htmlspecialchars($taak['username']);

    // Display
    echo("<li{$style}>{$taak['taak_naam']} - {$username}</li>");
}

By surrounding the string in double quotes ("), PHP recognizes variable names inside it and replaces them with their values.

The curly braces ({ and }) around the array values tell PHP the variable is $taak['taak_naam'], otherwise it finds only $taak and it outputs Array['taak_naam'] (which is not what we want).

If a value you use to create HTML content might contain characters that are special in HTML then you must encode those characters using their correct HTML definition. The PHP function htmlspecialchars() knows how to do it.

It encodes <, >, & and " (these are HTML breakers if they are not encoded properly). If you need to encode all the symbols that are defined in HTML as "character entities" you can use the PHP function htmlentities() instead.

axiac
  • 68,258
  • 9
  • 99
  • 134
0

you cannot use if between the strings

foreach ($dag as $taak) {
        $style='';
        if($_SESSION['user_id'] == $taak['username'])
          $style='style="color:red"';
        echo "<li {$style}> {$taak['taak_naam']} - {$taak['username']}</li>";
    }

                 ****OR****


 foreach ($dag as $taak) { ?>
   <li <?php if($_SESSION['user_id'] == $taak['username'])
      echo 'style="color:red"'; ?> >
      <?= $taak['taak_naam'].' - '.$taak['username']; ?>
   </li>';
  <?php
 }
Azmat Karim Khan
  • 437
  • 5
  • 18
-1

Forgot the semicolon.

foreach ($dag as $taak) {
echo '<li';
if($_SESSION['user_id'] == $taak['username']) {
    echo ' style="color:red"';
}
echo '>'.$taak['taak_naam'].' - '.$taak['username'].'</li>';
}
Hienz
  • 688
  • 7
  • 21
-1

You can use the short if syntax, the long syntax is not allowed in this context:

    foreach ($dag as $taak) {
        echo '<li' . (($_SESSION['user_id'] == $taak['username']) ? ' style="color:red"' : '') . '>' .$taak['taak_naam'] . ' - ' . $taak['username'].'</li>';
    }
Marvin Fischer
  • 2,552
  • 3
  • 23
  • 34
  • There is no such thing as *"the short if syntax"*. [`if`](http://php.net/manual/en/control-structures.if.php) is a control-flow statement, [`?:`](http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary) is an operator. They are completely different things. A statement does something (a control-flow statement decides what code branch to take based on a condition), an operator joins two sub-expressions into a larger expression. An expression has a value (it can be computed). – axiac May 22 '17 at 09:32