var 是否设置为与 undefined 相同的 null 以及如何检查差异 [英] Is var set to null same as undefined and how to check differences
问题描述
简单问题:在 php 中,var 是否设置为 null 与 undefined 相同?
Simple question: In php, is var set to null same as undefined?
另外,最简单的检查方法是var null是什么?
In addition, what is simplest way to check is var null?
如何在不破坏代码的情况下使用此功能,例如.通过在配置数组中设置一些可以设置为 null
的可选内容.
How to use this functionality without breaking code, eg. by setting something optional in config array that can be set to null
.
如果使用 is_null
检查 null
,如果没有定义 var,它可能会破坏代码.
If check for null
with is_null
it can potentially break code if var is not defined.
is_null
与 isset
完全相反,但仅当您确定定义了 var 时才能使用.Fore 所有其他情况看起来 isset
更合适.
is_null
is completely opposite of isset
, but can used only and only if you are certain that var is defined. Fore every other cases looks that isset
more appropriate.
并检查null
,isset
是不合适的,是吗?
And to check null
, isset
is unapropriate, is it?
所以 var 可以是:
So var can be:
- 未定义
- 空
- 空
- 带有要检查的内容
is_*
函数(numeric
、string
、array
、...)
- undefined
- null
- empty
- with content to check with
is_*
functions (numeric
,string
,array
,...)
推荐答案
var set to NULL 与 undefined 不同.
var set to NULL is not the same as undefined.
<?php
if (is_null($undefinedvariable)) {
echo 'This variable is NULL';
}
此代码演示了可能会如何引起混淆,因为它会导致显示消息此变量为 NULL",但也会生成未定义变量"通知.(当然,假设 $undefinedvariable 实际上是 undefined!)
This code demonstrates how confusion may arise, as it will result in the message 'This variable is NULL' being displayed, however it will also generate an "Undefined variable" notice. (assuming $undefinedvariable is actually undefined of course!)
这篇关于var 是否设置为与 undefined 相同的 null 以及如何检查差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!