PHP 如果不是空字符串并且字符串 =“this”或“那个”怎么办?
function test(){
$embedmode = 'normal';
if ( ( $embedmode != '' ) && ( $embedmode != 'normal' || $embedmode != 'popup' || $embedmode != 'popup' ) )
return "<p>ARVE Error: mode is not set to 'normal', 'popup' or 'special' maybe typo</p>";
elseif ( $embedmode == '')
$mode = 'default';
else
$mode = $embedmode;
echo '<pre>';
var_dump($mode);
echo "</pre>";
}
echo test();
这是我的尝试,我很头疼,现在它发出返回消息,我不知道为什么
function test(){
$embedmode = 'normal';
if ( ( $embedmode != '' ) && ( $embedmode != 'normal' || $embedmode != 'popup' || $embedmode != 'popup' ) )
return "<p>ARVE Error: mode is not set to 'normal', 'popup' or 'special' maybe typo</p>";
elseif ( $embedmode == '')
$mode = 'default';
else
$mode = $embedmode;
echo '<pre>';
var_dump($mode);
echo "</pre>";
}
echo test();
this is my attempt and I am getting a headache now it puts out the return message and I don't know why
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
!($var == 'something')
与($var != 'something')
相同。执行
(!$var == 'something')
会在进行比较之前对 $var 执行布尔运算。除非 $var 为空,否则 !$var 会返回 false,因此它本质上会说 (false == 'something'),这将是 false。!($var == 'something')
would be the same as($var != 'something')
.Doing
(!$var == 'something')
would perform the boolean operation on $var before doing the comparison. !$var would return false unless $var is empty, so it would essentially be saying (false == 'something'), which would be false.你的错误逻辑在这里:
如果
$embedmode
等于'normal'
,那么$embedmode != 'popup'
,所以这整个位都是 TRUE 。我相信你想替换||与 &&。对于更容易推理的代码,我可能会使用
in_array
或switch
,如下所示:Your bad logic is here:
if
$embedmode
equals'normal'
, then$embedmode != 'popup'
, so this whole bit is TRUE. I believe you want to replace || with &&.For code that's easier to reason about, I'd probably use
in_array
orswitch
, like this:1)
$var != 'something'
表示“$var IS NOT 'something'”。2)
!$var == 'something'
表示“$var 的否定是 'something'”。这些表达意味着不同的事情。用 $var is 'foobar' 来测试它,你的两个句子是:
1) 'foobar' IS NOT 'something',
2) THE NEGATION OF 'foobar' is 'something'
正如你所看到的,1) 将返回 true ,但是 2) 将返回 false,因为无论 'foobar' 是什么,它的否定,它仍然不等于 'something'。
1)
$var != 'something'
says "$var IS NOT 'something'".2)
!$var == 'something'
says "THE NEGATION OF $var IS 'something'".The expressions mean different things. Test it with saying $var is 'foobar', your two sentences are:
1) 'foobar' IS NOT 'something', and
2) THE NEGATION OF 'foobar' is 'something'
As you can see, 1) will return true, yet 2) will return false, because the negation of whatever 'foobar' is, it is still not equal to 'something'.