在echo内部连接PHP

wha*_*ale -2 php string string-concatenation

我试图在已打开的echo函数中添加if/else语句,但页面显示为空白.如果我正确连接,不太确定.

echo '<span class="year-tab-'.$x.' '.if ($single_year==$year_selected) { echo "" } else { echo "display-none" }.' ">';
Run Code Online (Sandbox Code Playgroud)

Joh*_*nde 5

你不能在连接中拥有控制结构

$style = ($single_year==$year_selected) ? '' : "display-none";
echo '<span class="year-tab-'.$x.' '.$style.'">';
Run Code Online (Sandbox Code Playgroud)

这种连接有点混乱.printf() 可能会更清洁

printf('<span class="year-tab-%s %s">', 
    %x,
    ($single_year==$year_selected) ? '' : "display-none"
);
Run Code Online (Sandbox Code Playgroud)