PHP变量字符串中的IF/ELSE语句

Ric*_*701 1 html php if-statement

在变量字符串中使用IF/ELSE语句的正确方法是什么?

例:

$htmlOutput = 'The current color of the sky is ' . 
if ($time==="day") { . 'blue.' . } 
else if ($time==="night") { . 'black' . };
Run Code Online (Sandbox Code Playgroud)

显然这个例子不起作用,但是你看到我正在尝试做什么.我知道我可以继续if语句中的变量,如:

$htmlOutput .= '';
Run Code Online (Sandbox Code Playgroud)

但我很好奇是否有办法如上所述.

Bru*_*lza 5

你可以使用ternary operator这样的

$htmlOutput = 'The current color of the sky is ' . ($time == 'day' ? 'blue' : 'black');
Run Code Online (Sandbox Code Playgroud)