Candice Horie
Candice Horie

Reputation: 49

Setting a default value if a variable is empty

I have one field Style, in this field I have few options to select, but what I want is when the person doesn't select any style, in the field I want to display the text "N/A" How I can do that?

Now display nothing if i dont select the style Style Option

Style Edit

<dt> Style </dt>
<dd> <?php $styles=styles(); 
if(isset($styles[$user->style]))echo $styles[$user->style];
?> </dd>

Upvotes: 1

Views: 43

Answers (1)

Nick
Nick

Reputation: 147156

If you're using PHP7, you can use the null coalescing operator to give a default value when a variable isn't set:

echo $styles[$user->style] ?? 'N/A'

In earlier versions, use the ternary operator:

echo isset($styles[$user->style]) ? $styles[$user->style] : 'N/A';

Upvotes: 1

Related Questions