tags:

views:

78

answers:

3
if ($row['active'] == 1) echo '<a href="prof?id=$id">'.htmlspecialchars($row['username']).'</a>'; else echo htmlspecialchars($row['username']);

Could I write this shorter and cleaner somehow?

+4  A: 
echo $row['active'] == 1 ?  '<a href="prof?id=$id">'.htmlspecialchars($row['username']).'</a>' : htmlspecialchars($row['username']);

explained a little here http://www.addedbytes.com/php/ternary-conditionals/

John Boker
A: 

You could take advantage of the ternary operator:

echo ($row['active'] == 1) 
    ? '<a href="prof?id=$id">'.htmlspecialchars($row['username']).'</a>'
    : htmlspecialchars($row['username'])
;

(I split the code onto separate lines for the sake of formatting.

zombat
+1  A: 

I'm assuming you made a mistake putting the $id in a single quoted string, and meant for php to put the value of $id in its place in there.

$name=htmlspecialchars($row['username']);
if($row['active'] == 1) {
    echo "<a href='prof?id=$id'>$name</a>";
} else {
    echo $name;
}
David