如何在一个标签中使用多个 "if"

How can use multi "if" in one tag

如何在一个标签 like <header> 中使用多个 "if"。 每个 div 都与一个选项卡相关。所以我想为每个选项卡设置一个显示条件。

<?php     
$mainbox = get_field('on');
$guide = get_field('install_guide');
$required = get_field('required');
?>
<section class="fieldset"> 
<header>
<?php
if (!empty($mainbox)){
<div class="download-taber active"> tab1</div> //// if true show this otherwise hide it
<div class="attr-taber">tab2</div>} //// if true show this otherwise hide it

elseif (!empty($guide)){ 
<div class="install-guide-taber">tab3</div>} //// if true show this otherwise hide it

elseif (!empty($required)){
  <div class="system-required-taber">tab4</div>} //// if true show this otherwise hide it
?>  
</header>
</section>
<?php endif; ?>

简单(列出语句以便于交换值以进行测试):

<?php                   
list($mainbox, $guide, $required)  = [true, true, true];
?>
<section class="fieldset">  
    <header>
    <?php if($mainbox) { ?>
        <div class="download-taber active">tab1</div>
        <div class="attr-taber">tab2</div>
    <?php } ?>
    <?php if($guide) { ?>
        <div class="install-guide-taber">tab3</div>
    <?php } ?>
    <?php if($required) { ?>
        <div class="system-required-taber">tab4</div>
    <?php } ?>      
    </header>
</section>

如果所有变量都设置为 true,将显示所有选项卡。

要在 PHP 中嵌入 html,您必须将 html 代码保留在 php 的回显标记中,试试这个。

 <?php                  
    $mainbox = get_field('on');
    $guide = get_field('install_guide');
    $required = get_field('required');
    ?>
    <section class="fieldset">  
    <header>
    <?php
    if (!empty($mainbox)){
    echo "<div class='download-taber active'>tab1</div>"; //// if true show this otherwise hide it
    echo "<div class='attr-taber'>tab2</div>";} //// if true show this otherwise hide it

    elseif (!empty($guide)){    
    echo "<div class='install-guide-taber'>tab3</div>";} //// if true show this otherwise hide it

    elseif (!empty($required)){
            echo "<div class='system-required-taber'>tab4</div>";} //// if true show this otherwise hide it
    ?>      
    </header>
    </section>
    <?php endif; ?>