This question already has an answer here:
Ok the below information works im just trying to figure my problem is that if that it displays like this title grade...
but i want to put a | inbetween like this
title | grade.
my below code works but if there is no grade it still echos the |,
how can i get it to only echo when there is a value in that field.
because when the field is empty it shows
title |
<div class="sto-info">
<span><?php echo $title; ?> <?php echo '|', $grade; ?></span>
</div>
</div>
You can use a ternary operator:
<span><?php echo $title; ?> <?php echo $grade ? '|' . $grade : ''; ?></span>
Absolutely what Dave said is correct. You can use "IF Statement" too if you aren't familiar with this operator.
if ($grade !== '') {
echo $title.', | ,'.$grade;
} else {
echo "No Grade !";
}
You can use the isset() or is_null() functions
<span><?php echo $title, !is_null($grade) ? '|' . $grade : '' ; ?></span>
or:
<span><?php echo $title, isset($grade) ? '|' . $grade : '' ; ?></span>
If grade exists but is empty then you need another test, for example:
<span><?php echo $title, isset($grade) && strlen($grade) > 0 ? '|' . $grade : '' ; ?></span>