为什么我的变量匹配错误的if()条件?

Daa*_*ice -1 php conditional-statements operator-keyword

我有

echo $role;

if  (!$role=='trial_expired') { echo 'not expired '; } else { echo 'expired '; }
Run Code Online (Sandbox Code Playgroud)

$ Role输出'trial',根据数据库是正确的userrole,但是后面的条件仍然返回false,返回'expired'.出于某种原因,'试用'(我的猜测因为它部分是'trial_expired')被视为与trial_expired相同.有什么操作员可以解决这个问题?

esq*_*qew 5

您将!逻辑运算符置于错误的位置.为了比较,如果字符串相等的,你需要有!(操作员立即放在左边的平等比较==).

if ($role!=='trial_expired') { echo 'not expired '; } else { echo 'expired '; }
Run Code Online (Sandbox Code Playgroud)

  • @ thecoder16`!==`[有效](http://www.php.net//manual/en/language.operators.comparison.php).它比较了类型和相等,似乎这就是OP试图做的事情,只是在错误的地方做出否定. (3认同)