Search code examples
phpternary-operatorvbulletin

Use a ternary operator in a variables string


Is it possible to use a ternary operator inside of a variable that also is checking a variable, I'm not sure if that is the correct way to explain, so let me just show you what I mean.

$drc_embed_vine =  '<iframe src="https://vine.co/v/$2/embed/'.($vbulletin->options['drc_embed_vine_smpl']) ? simple : postcard.'" width="480" height="480" frameborder="0"></iframe>';

This var is just checking whether the setting is on or off, and is a boolean returning a 1 or a zero

$vbulletin->options['drc_embed_vine_smpl']

But that's useless information I just need to know how or if I can use a ternary operator here.

Basically I am trying to simplify this:

if ($vbulletin->options['drc_embed_vine_smpl']) {
    $drc_embed_vine =  '<iframe src="https://vine.co/v/$2/embed/simple" width="480" height="480" frameborder="0"></iframe>';
  } else {
    $drc_embed_vine =  '<iframe src="https://vine.co/v/$2/embed/postcard" width="480" height="480" frameborder="0"></iframe>';
  }

If this is possible, what am I doing wrong?


Solution

  • You have to enclose those inside paranthesis. In addition, you are missing quotes for simple and postcard:

    $drc_embed_vine = '<iframe src="https://vine.co/v/$2/embed/'.(($vbulletin->options['drc_embed_vine_smpl']) ? "simple" : "postcard").'" width="480" height="480" frameborder="0"></iframe>';
                                                                 ^                                               ^          ^         ^