2016-02-11 157 views
1

回聲語句中的IF條件不起作用。如果回聲內條件

我得到這個錯誤:

Syntax error, unexpected ')' in

echo ' <div class="panel-body"> 
     '.$dec.' 
     '.(($ttype == "video") 
      ? '<iframe class="embed-responsive-item" 
       width="560" height="315" 
       src="https://www.youtube.com/embed/'.$only_id[1].'" frameborder="0" 
       allowfullscreen=""></iframe>').' 
       </div>'; 
+0

如果失敗情況'​​你應該添加另一個值 ': 「」)' – roullie

+0

試試這個:'回聲'

> '.$dec.' > \t \t \t \t \t ('.$ttype.' == "video")?').' >
「;' –

+0

試試這個: - 'echo'

'.$dec.(($ttype == "video"))?':""
';' –

回答

1

你可以用變量串聯。這將幫助你避免混亂

$html = ''; 
$html .= '<div class="panel-body">'; 
$html .= $dec; 
$html .= ($ttype == "video")?'<iframe class="embed-responsive-item" width="560" height="315" src="https://www.youtube.com/embed/'.$only_id[1].'" frameborder="0" allowfullscreen=""></iframe>':'<!-- else part -->'; 
$html .= '</div>'; 
echo $html; 
1

讓你的生活簡單,使用這樣的:

<div class="panel-body"> 
<?php echo $dec; ?> 
<?php 
(($ttype == "video") ? '<iframe class="embed-responsive-item" width="560" height="315" 
src="https://www.youtube.com/embed/'.$only_id[1].'" frameborder="0" allowfullscreen=""></iframe>' : ''); 
?> 
</div> 

在代碼中,你缺少三元運營商的其他條件。

解決方案與您的代碼:

echo ' 
    <div class="panel-body">'.$dec.' 
    '.(($ttype == "video") ? ' 
     <iframe class="embed-responsive-item" width="560" height="315" 
     src="https://www.youtube.com/embed/'.$only_id[1].'" frameborder="0" allowfullscreen=""> 
     </iframe>' : ''). 
    '</div>';