dsue14118 2018-04-19 16:04
浏览 30
已采纳

PHP elseif问题

I'm trying to add col-lg-6 to one of the Divs when no content is being displayed for name3 and name4, but it keeps showing as col-lg-4.

<?php
$name3 = get_field('name3', $post->ID);
$name4 = get_field('name4', $post->ID);
?>

<div class="col-lg-3">
    <!-- name1 exists -->
</div>

<?php if ($name3 == "" || $name4 == ""): ?>
    <div class="col-lg-4">
    <?php elseif ($name3 == "" && $name4 == ""): ?>
        <div class="col-lg-6">
        <?php else: ?>
            <div class="col-lg-3">
            <?php endif; ?>
            <!-- name2 exists -->
        </div>

        <div class="col-lg-3">
            <!-- name3 does not exist -->
        </div>

        <div class="col-lg-3">
            <!-- name4 does not exist -->
        </div>

I've looked on here and other sites but I just can't figure it out. I know it's probably going to be something stupid and simple but I really need to sort this out now.

  • 写回答

2条回答 默认 最新

  • dongzichan2886 2018-04-19 16:12
    关注

    Simply reverse order of checks:

    // if both are empty
    <?php if($name3 == "" && $name4 == ""): ?>
    <div class="col-lg-6">
    // if one of them is empty
    <?php elseif($name3 == "" || $name4 == ""): ?>
    <div class="col-lg-4">
    <?php else: ?>
    <div class="col-lg-3">
    <?php endif; ?>
    

    otherwise first if () is true when one of variables is empty and if both are empty

    also you can just change || (or) operator to xor operator in your first if ()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大