PHP字符串等于检查失败


PHP string equals check fails

我有一个奇怪的问题。我检查了一个php字符串,如下所示:

在第1页

$_SESSION['test']=<a value from a row fetched from db>

第2页

$myVar=$_SESSION['test'];
echo $myVar;
if($myVar=="This is the match string"){
echo "Matched";
}else{
echo "Not Matched";
}

我可以看到$myVar重复了我再次检查的同一个字符串"This is the match string",但它仍然进入其他字符串。我试过了:

$myVar=(string)trim($_SESSION['test']);

但它仍然会进入其他领域。问题出在哪里?知道吗?

后面可能有空格(空格、制表符、换行符)。

尝试

$myVar = trim( $_SESSION['test'] );

正在执行var_dump($_SESSION['test']);也可能揭示问题。

您确定您的字符串真的相等吗?try var_dump($_SESSION['test']);并比较两个长度和字母。