PHP 真值与空值

本文参考 http://php.net/manual/en/types.comparisons.php

1. isset

bool isset ( mixed $var [, mixed $... ] )

Determine if a variable is set and is not NULL.
变量值被设置,而且其值不为 NULL,那么就会返回 true。

另外这个函数可以被用来检查数组中的元素是否存在且值不为 NULL。

2. empty

bool empty ( mixed $var )

A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.
变量不存在或者其值为 FALSE,empty 函数都返回真。另外 empty 只能处理变量,无法处理表达式的结果。

3. PHP 中一些奇怪的值【背下来】

复制代码
1 $x = '';                // 为 false
2 $x = null;              // 没有值,没有类型,为 false
3 var $x;                 // 没有值,没有类型,为 false
4 $x is undefined;        // 没有值,没有类型,为 false
5 $x = array();           // 为 false
6 $x = false;             // 为 false
7 $x = 0;                 // 为 false
8 $x = '0';               // 为 false
9 $x = 'false'            // 为 true
复制代码

上面这些值得真值测试都是 loose comparison。

posted @   WendellYih  阅读(198)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
点击右上角即可分享
微信分享提示