PHP If语句始终触发为true [英] PHP If statement always firing as true

查看:229
本文介绍了PHP If语句始终触发为true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我知道这已经被问了一百万次,但我找到的答案似乎都没有用。我在'Header.php'页面中有以下if语句:

First of all, I know that this has already been asked a million times, but none of the answers I have found seem to work. I have the following if statement in my 'Header.php' page:

<?php
$currentpage = $_SERVER['REQUEST_URI'];
    echo($currentpage);
    if($currentpage == "/recap-2013.php" || "/index.php" ) { ?>
        <script src=... a bunch of HTML
<?php } ?>

如果$ currentpage等于recap-2013.php或index,则插入一些JS .php,如果没有,什么都不做。

The idea is to insert some JS if $currentpage is equal to either recap-2013.php or index.php, and if not, to do nothing.

我添加了 echo($ currentpage) in那里检查我的 REQUEST_URI 是什么。

I've added the echo($currentpage) in there to check what my REQUEST_URI is.

问题是,即使请求URI是/someotherpage.php ,该语句的计算结果为true,JS出现在我的head标签中。我确定这是一个很小的东西我不知道但我无法弄清楚是什么。

The problem is, even if the request URI is /someotherpage.php, the statement evaluates as true and the JS appears in my head tag. I'm sure it's something small that I'm missing but I cannot figure out what.

提前感谢您的帮助!

推荐答案

你的if语句逻辑是不正确的。你的意思是:

Your if statement logic is incorrect. What you really mean is:

if($currentpage == "/recap-2013.php" || $currentpage == "/index.php" ) { ?>

在你当前的逻辑中,第二个表达式将永远为真,因为PHP有​​松散的输入,字符串将被评估作为布尔值和非空字符串始终为真。

In your current logic the second expression will always be true because PHP has loose typing the string will be evaluated as a boolean and a non-empty string is always true.

这篇关于PHP If语句始终触发为true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆