PHP strcmp没有使用会话变量返回正确的答案 [英] PHP strcmp is not returning the correct answer with session variable

查看:92
本文介绍了PHP strcmp没有使用会话变量返回正确的答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个会话变量$ _SESSION ['condition'],我知道它已设置,因为当我输入此变量时:

I have a session variable $_SESSION['condition'], I know it is set because when I entered this:

echo $_SESSION['condition']." = "."Below Average";

它返回:

Below Average = Below Average

当我在会话变量上执行gettype()时,它返回类型"string".

When I do a gettype() on the session variable it returns type "string".

但是当我执行strcmp()时,它返回:-34

Yet when I do strcmp() it returns: -34

我还尝试过使用==而不是用strcmp测试IF语句是否相等,并且还使用IF语句将它们都转换为字符串并测试它们是否相等而没有运气.

I have also tried an IF statement with == rather than strcmp testing for equality AND an IF statement casting them both as strings and testing if they are equal with no luck.

为什么会这样?

推荐答案

可能存在空格或不可见字符,导致strcmp()出现此问题.您可以使用trim()帮助清理字符串,然后使用文字方程运算符===来测试真实相等性.见下文

There could be a whitespace or invisible character that is causing this problem with strcmp(). You can use trim() to help clean up strings and then use a literal equation operator, === to test for true equality. See below

$condition = trim($_SESSION['condition']);

if ($condition === 'Below Average') {
    echo 'True';
} else {
    echo 'Nope!';
}

看看是否有帮助.

此外,您可以使用var_dump($ _ SESSION ['condition']);检查值.

Also, you can use var_dump($_SESSION['condition']); to inspect the value.

这篇关于PHP strcmp没有使用会话变量返回正确的答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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