我正在将表单中的数据存储在 Concrete5 中的用户数据中。
我已成功提取用户数据:
$u = new User();
$ui = UserInfo::getByID($u->getUserID());
$testtype = $ui->getAttribute('TestType','display');
这返回了我所期望的。 但是当我尝试使用...
} else if ($testtype == "English Adult Male") {
。它不会触发。
我回显我正在拉取的输出,并注意到 html 在变量的回显之后放了一个<br>
。 我试图通过trim
传递输出,但结果相同。
我做错了什么?
使用===
而不是==
进行比较,作为一种很好的做法。更多阅读 这里
以这种方式更改代码,使其中$case
字符串,并执行var_dump
$case
和$testtype
<?php
$testtype = "English Adult Male ";
$case = "English Adult Male ";
var_dump($testtype);
var_dump($case);
if ($testtype === "whatever") {
echo "IF!";
} else if ($testtype === $case) {
echo "ELSE IF!";
}
上面的代码生成输出:
string(19) "English Adult Male "
string(19) "English Adult Male "
ELSE IF!
注意
字符串(19( 非字符串(23(
您的 var 转储中有 23 个,它超过了字符串中的字符数"English Adult Male "
这让我得出结论,您有一个多字节字符编码,但您可能正在使用不是多字节而是单字节的字符串及其 19 字节字符串与 23 字节字符串进行测试。
您可以通过mb_convert_encoding((转换$testtype
及其支持的编码
mb_detect_encoding也可以方便地检测$testtype
的编码;
问题出在行上
$testtype = $ui->getAttribute('TestType','display');
通过将其更改为:
$testtype = $ui->getAttribute('TestType');
然后将条件设置为 ==
,因为===
不起作用,结果是如愿以偿的。
希望这对某人有所帮助!