0

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>
Geek
  • 27
  • 7

3 Answers3

2

You can use a ternary operator:

<span><?php echo $title; ?> <?php echo $grade ? '|' . $grade : ''; ?></span>
dave
  • 62,300
  • 5
  • 72
  • 93
2

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 !";
}
1

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>
Dominique Lorre
  • 1,168
  • 1
  • 10
  • 19