PHP 如果不是空字符串并且字符串 =“this”或“那个”怎么办?

发布于 2024-12-29 16:56:21 字数 477 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷…雨湿花 2025-01-05 16:56:21

!($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.

乜一 2025-01-05 16:56:21

你的错误逻辑在这里:

( $embedmode != 'normal' || $embedmode != 'popup' || $embedmode != 'popup' )

如果 $embedmode 等于 'normal',那么 $embedmode != 'popup',所以这整个位都是 TRUE 。我相信你想替换||与 &&。

对于更容易推理的代码,我可能会使用 in_arrayswitch,如下所示:

switch ($embedmode) {
  case 'normal':
  case 'popup':
  case 'special':
    // valid, proceed
    break;
  case '':
    $embedmode = 'default';
    break;
  default:
    trigger_error ("Embed mode '$embedmode' not valid.");
    break;
}

Your bad logic is here:

( $embedmode != 'normal' || $embedmode != 'popup' || $embedmode != 'popup' )

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 or switch, like this:

switch ($embedmode) {
  case 'normal':
  case 'popup':
  case 'special':
    // valid, proceed
    break;
  case '':
    $embedmode = 'default';
    break;
  default:
    trigger_error ("Embed mode '$embedmode' not valid.");
    break;
}
書生途 2025-01-05 16:56:21

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'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文