【问题标题】:CodeIgniter 3: Multiple condition in if statement not working in PhpCodeIgniter 3:if 语句中的多个条件在 Php 中不起作用
【发布时间】:2017-10-02 13:11:44
【问题描述】:

我是 CodeIgniter 的新手。我的视图文件中有 if/if else 语句和 || 之后的代码不工作

这是我认为的代码

<?php if (($destination["site"]!="block_view/profile/profile")||($destination["site"]!="block_view/profile/slips")){
?>


<section id="profilephp-header" style="background-image: url(<?php echo base_url('assets/img/parallax/header-profile.jpg'); ?>);" class="parallax">
    <div class="overlay">
        <div class="container">
            <div class="row">
                <div class="sec-title text-center wow animated fadeInDown">
                    <img src="<?php echo base_url('assets/img/parallax/profile-text.png'); ?>" alt="">
                </div>
            </div>
        </div>
    </div>
</section>
<?php 
}else if(($destination["site"]=="block_view/profile/profile")||($destination["site"]=="block_view/profile/slips")){?>
    <section id="profilephp-header2" style="background-image: url(<?php echo base_url('assets/img/parallax/header-profile.jpg'); ?>);" class="parallax">
        <div class="overlay">

        </div>
    </section>
<?php
}?>

似乎 if 语句只接受一个条件。我的视图文件中的所有条件都是这样的。

【问题讨论】:

  • 但是两个 if 语句是一样的吗?我错过了什么吗?
  • 不,它们不是,但看到声明,$destination["site"] 只能有一个值,要么不是 "block_view/profile/profile" 要么 不是 "block_view/profile/slips" 所以这个 @987654325 @语句总是true

标签: php codeigniter if-statement


【解决方案1】:

正如我在评论中所说,$destination["site"] 总是要么

  • 不是"block_view/profile/profile"
  • 不是"block_view/profile/slips"
    所以你的if 语句总是true

你可能想写

if (($destination["site"]!="block_view/profile/profile")&&($destination["site"]!="block_view/profile/slips"))

而不是

if (($destination["site"]!="block_view/profile/profile")||($destination["site"]!="block_view/profile/slips"))

(将|| - OR - 替换为&amp;&amp; - AND)


补充我的答案:

根据您的问题,您似乎认为 !a || !b!(a || b) 相同。

您必须知道,当您否定这样的大语句时,您必须在分配否定时还原 &amp;&amp;|| 操作数。

基本示例:

  • 我想找一只既不是红色也不是蓝色的袜子 (!(red || blue))
  • 一样
  • 我想找一只既不是红也不是蓝的袜子 (!red &amp;&amp; !blue)

【讨论】:

    【解决方案2】:

    $destination['site'] 有一个值,但在您的第一个 if 语句中,您正在检查它是否不等于一个事物,或者它不等于另一个事物。这将永远是真的!

    如果目标站点不是这两个站点,您似乎打算显示该部分,因此您可以将其重写为:

    <?php if (!in_array($destination['site'], ['block_view/profile/profile', 'block_view/profile/slips'])): ?>
    <section id="profilephp-header" style="background-image: url(<?php echo base_url('assets/img/parallax/header-profile.jpg'); ?>);" class="parallax">
        <div class="overlay">
            <div class="container">
                <div class="row">
                    <div class="sec-title text-center wow animated fadeInDown">
                        <img src="<?php echo base_url('assets/img/parallax/profile-text.png'); ?>" alt="">
                    </div>
                </div>
            </div>
        </div>
    </section>
    <?php else: ?>
        <section id="profilephp-header2" style="background-image: url(<?php echo base_url('assets/img/parallax/header-profile.jpg'); ?>);" class="parallax">
            <div class="overlay">
    
            </div>
        </section>
    <?php endif; ?>
    

    【讨论】:

    • 有效!太感谢了。我现在明白了,谢谢你的解释。
    【解决方案3】:

    将您的 <?php if (($destination["site"]!="block_view/profile/profile")||($destination["site"]!="block_view/profile/slips")){ ?> 替换为 &amp;&amp; 条件,您可能会对这两者感到困惑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-20
      相关资源
      最近更新 更多