dungey_140
dungey_140

Reputation: 2792

Converting if/else to ternary operator

I'm just getting to grips with ternary operators in PHP and am having trouble converting the following:

<? if( $link['file'] ): ?>
  <?=$link['file'] ?>
<? else: ?>
  <?=$link['link'] ?>
<? endif ?>

My attempt so far, that generates an error:

<? if( $link['file'] ) ? $link['file'] : $link['link']; ?>

Any pointers how to get this conversion working, it must be close?

Upvotes: 1

Views: 62

Answers (1)

u_mulder
u_mulder

Reputation: 54831

With shorthand form ?:

<?= $link['file'] ?: $link['link'] ?>

Full form:

<?= $link['file'] ? $link['file'] : $link['link'] ?>

Upvotes: 4

Related Questions