user2025469
user2025469

Reputation: 1581

replace if statements by conditional operators in php variable

I need to change my PHP code from: ($a contains parsed text with html tags to which I want to append more data, checking if video links exist, append the echo statement)

$cont = '<div>'.$a.'<br>.'
  if (!empty($a->find('object', 0)->data)){
    echo '<a target="_blank" href="'.$a->find('object', 0)->data.'">
    Play Video</a>&nbsp;&nbsp;';
  }
  if (!empty($a->find('object', 1)->data)){
    echo '<a target="_blank" href="'.$a->find('object', 1)->data.'">
    Play Video</a>&nbsp;&nbsp;';
  }
.'</div>';

I know this is wrong so I need help implementing comparison operators. Something like:

$cont = '<div>.'$a.'<br>'.
  ($a->find('object', 0)->data > 0 ? '<a target="_blank" 
  href="'.$a->find('object', 0)->data.'">Play Video</a>&nbsp;&nbsp;')
  ($a->find('object', 1)->data > 0 ? '<a target="_blank" 
  href="'.$a->find('object', 1)->data.'">Play Video</a>&nbsp;&nbsp;')
  .'</div>';

But I get a syntax error "(" by the first $a->find. Is it possible to do something like this?

Upvotes: 0

Views: 639

Answers (1)

bwoebi
bwoebi

Reputation: 23787

You have to add the else part introduced by a semicolon:

$cont = '<div>'.$a.'<br>'.
  ($a->find('object', 0)->data > 0 ? '<a target="_blank" 
  href="'.$a->find('object', 0)->data.'">Play Video</a>&nbsp;&nbsp;':'').
  ($a->find('object', 1)->data > 0 ? '<a target="_blank" 
  href="'.$a->find('object', 1)->data.'">Play Video</a>&nbsp;&nbsp;':'') // :'' here
  .'</div>';

Upvotes: 1

Related Questions