在HTML页面上添加彩色按钮if语句[关闭]

I want to add different color buttons on HTML page inside a if statement . Green for when condition is true , red for no

In PHP?

if(statement) { 
   echo '<button style="background: green;">BUTTON</button>';
} else { 
   echo '<button style="background: red;">BUTTON</button>';
}

This should work:

if(condition1 === true)
{
    echo "<button style=\"background-color: green;\">Green</button>";
}
else
{
    echo "<button style=\"background-color: red;\">Red</button>";
}

Or a different way, easy when you need to print alot of html:

if(condition1 === true)
{
    ?>
    <button style="background-color: green;">Green</button>
    <?php
}
else
{
    ?>
    <button style="background-color: red;">Red</button>
    <?php
}

Alternatively you can use the ternary operator:

echo '<button style="background-color:'. ( $condition ? '#ff0000' : '#00ff00' ) .';">Button</button>';