使用逻辑或设置变量

发布于 2024-12-20 21:18:08 字数 310 浏览 4 评论 0原文

$db_hased_pw = $result["password"] || false;

我理解这行代码的用法,但是什么时候它会评估为 false?

仅当 $result["password"] 未定义时,$db_hased_pw 才等于 false 吗?

或者如果 $result["password"] 未设置、或 false、零或 null,则 $db_hased_pa​​ssword 是否设置为 false?

$db_hased_pw = $result["password"] || false;

I understand the usage of this line of code, but WHEN will it evaluate as false?

will $db_hased_pw equal false only when $result["password"] is undefined?

or is $db_hased_password be set to false if $result["password"] is unset, or false, or zero, or null?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

泪是无色的血 2024-12-27 21:18:08

当 $result["password"] 是“假值”时,它将计算为 false。 empty 上的页面描述了这些值。

这些是等效的:

$db_hased_pw = !!$result["password"];
$db_hased_pw = (bool) $result["password"];

如果 $result["password"] 确实可以是未定义的,您应该使用:

$db_hased_pw = !empty($result["password"]);

以避免通知。

It will evaluate to false when $result["password"] is a "falsy value". The page on empty describes these values.

These would be equivalent:

$db_hased_pw = !!$result["password"];
$db_hased_pw = (bool) $result["password"];

If $result["password"] can indeed be undefined, you should be using:

$db_hased_pw = !empty($result["password"]);

to avoid a notice.

梦回梦里 2024-12-27 21:18:08

来自 PHP 文档:

当转换为布尔值时,以下值被视为 FALSE:

  • 布尔值 FALSE 本身
  • 整数 0(零)
  • 浮点数 0.0(零)
  • 空字符串,以及字符串“0”
  • 具有零个元素的数组
  • 一个对象零成员变量(仅限 PHP 4)
  • 特殊类型 NULL(包括未设置的变量)
  • 从空标签创建的 SimpleXML 对象

您可以在 页面。

From PHP documentation:

When converting to boolean, the following values are considered FALSE:

  • the boolean FALSE itself
  • the integer 0 (zero)
  • the float 0.0 (zero)
  • the empty string, and the string "0"
  • an array with zero elements
  • an object with zero member variables (PHP 4 only)
  • the special type NULL (including unset variables)
  • SimpleXML objects created from empty tags

You can read more on this page.

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