【问题标题】:If else not running on Boolean value [closed]如果否则不在布尔值上运行[关闭]
【发布时间】:2020-10-20 05:11:06
【问题描述】:

尝试了以下代码。我所能得到的只是“不工作”。

bp_is_my_profile();        //this is a Boolean.
echo bp_is_my_profile();      // this echo's out either one or nothing depending on true or false.

if ($bp_is_my_profile === "true") {      // I've tried putting in numbers one and zero.
  echo "Have a good morning!";
} elseif ($bp_is_my_profile === "false") {  // I've tried without quotes.
  echo "Have a good day!";
} else {
  echo "Not working";      //whatever the Boolean is, each's it prints this line of code.
}

【问题讨论】:

  • $bp_is_my_profile 未定义。您还没有在样本中的$bp_is_my_profile 中存储任何东西。
  • $bp_is_my_profile = bp_is_my_profile();
  • 运行函数不会自动设置变量。
  • 正如@s.wadhwa 指出的那样。您需要将该行代码放在if-else 之前,还将您的true-false 字符串更改为布尔值。
  • === 也会检查值和数据类型。即使$bp_is_my_profiletrue$bp_is_my_profile === "true" 也会返回false,因为"true" 是字符串类型。

标签: php function if-statement boolean


【解决方案1】:

调用if中的函数

if (bp_is_my_profile()) {
    echo "Have a good morning";
} else {
    echo "Have a good day";
}

您不应该与字符串进行比较,因为该函数返回一个布尔值。而且由于它返回一个布尔值,所以只有两种可能,所以你不需要三种情况。

【讨论】:

    猜你喜欢
    • 2016-04-02
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2013-09-02
    • 1970-01-01
    相关资源
    最近更新 更多