php - 将 CSS 类作为 PHP if else 语句的一部分
问题描述
有没有一种好方法可以将 css 类作为 PHP if else 语句的一部分调用?我要做的是根据 php if else 语句的结果值更改作为 html 一部分的 div 的背景颜色。
php
if(!empty($check_availability)){
echo <div id="1"> do something <div>
// make the div background green if this is true
}
else {
<div id="2"> do something else instead<div>
//make backgound red if this is true
}
html
<div id="container"> <!-- <<<<<<< this is the div that I want to change the background color of depending on results from the php -->
<div id="1"></div>
<div id="2"></div>
</div>
解决方案
我假设您正在谈论有条件地向 div 添加类。good
是主观的,但这是我通常做的。
$class = !empty($check_availability) ? 'green-class' : 'red-class';
echo "<div class='{$class}'> do something <div>";