CodeIgniter 3:if语句在Php中不起作用的多个条件
I'm new with CodeIgniter. I have an if/if else statement in my view file and the codition after || is not working
Here's a code from my view
<?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
}?>
It seems like the if statement is only accepting one condition. All the conditions in my view files is like this.
我是CodeIgniter的新手。 我在视图文件中有一个if / if else语句,在||之后有一个编码 无法正常工作 p>
以下是我视图中的代码 strong> p>
似乎if语句只接受一个条件。 我的视图文件中的所有条件都是这样的。 p>
div>
&lt;?php if(($ destination) [ “现场”] = “block_view /简档/简档”)||($目的地[ “现场”] = “block_view /简档/单”)){
&GT;!吗?
&lt;节 id =“profilephp-header”style =“background-image:url(&lt;?php echo base_url('assets / img / parallax / header-profile.jpg');?&gt;);” class =“parallax”&gt;
&lt; div class =“overlay”&gt;
&lt; div class =“container”&gt;
&lt; div class =“row”&gt;
&lt; div class = “sec-title text-center wow animated fadeInDown”&gt;
&lt; img src =“&lt;?php echo base_url('assets / img / parallax / profile-text.png');?&gt;” alt =“”&gt;
&lt; / div&gt;
&lt; / div&gt;
&lt; / div&gt;
&lt; / div&gt;
&lt; / section&gt;
&lt;?php
}其他如果 (($ destination [“site”] ==“block_view / profile / profile”)||($ destination [“site”] ==“block_view / profile / slips”)){?&gt;
&lt; section id =“profilephp-header2”style =“background-image:url(&lt;?php echo base_url('assets / img / parallax / header-profile.jpg');?&gt;);” class =“parallax”&gt;
&lt; div class =“overlay”&gt;
&lt; / div&gt;
&lt; / section&gt;
&lt;?php
}?&gt;
code> pre>
$destination['site']
has one value, but in your first if-statement, you're checking if it's either not equal to one thing, or it's not equal to another thing. That'll always be true!
It looks like you were intending to show that section if the destination site was neither of those, so you could rewrite it as this:
<?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; ?>
As I said in my comment, $destination["site"]
is always either
-
not
"block_view/profile/profile"
or -
not
"block_view/profile/slips"
so yourif
statement is alwaystrue
.
You might want to write
if (($destination["site"]!="block_view/profile/profile")&&($destination["site"]!="block_view/profile/slips"))
instead of
if (($destination["site"]!="block_view/profile/profile")||($destination["site"]!="block_view/profile/slips"))
(replace ||
- OR - with &&
- AND)
Addition to my answer :
You seemed - as per your question - to believe that !a || !b
is the same as !(a || b)
.
You have to know that when you negate a big statement like this, you have to revert &&
and ||
operands as you distribute the negation.
Basic example :
- I want to find a sock which is not red or blue (
!(red || blue)
)
is the same as - I want to find a sock which is neither red nor blue (
!red && !blue
)
replace your <?php if (($destination["site"]!="block_view/profile/profile")||($destination["site"]!="block_view/profile/slips")){
?>
with &&
condition you might got confused with the two.