简体   繁体   English

正确的使用方法!is_null()

[英]Correct way to use !is_null()

There is an example in PHP Object-Oriented Solutions that seems to be flawed, if I understand is_null properly. 如果我理解is_null,PHP面向对象解决方案中有一个例子似乎有缺陷。

The sample code in the book is as follows: 书中的示例代码如下:

if (!is_null($required) && !is_array($required)) {
throw new Exception('The names of required fields must be an array, even if only one field is required.');
}

This code is supposed to test that a var $required is not NULL and is an array. 这段代码应该测试var $ required不是NULL并且是一个数组。 To my understanding, is_null() returns TRUE if the variable is not set or is NULL. 据我所知,如果未设置变量或is为NULL,则is_null()返回TRUE。 So, does it make sense to negate is_null() in that example if you're trying to throw an exception when the variable is not set, NULL, or is not an array? 那么,如果你在没有设置变量,NULL或不是数组的情况下尝试抛出异常,那么在该示例中否定is_null()是否有意义? The only way an exception is thrown is if (true && true) is satisfied. 抛出异常的唯一方法是if(true && true)是否满足。

FROM: http://www.php.net/manual/en/language.types.null.php 来自: http//www.php.net/manual/en/language.types.null.php

A variable is considered to be null if: 在以下情况下,变量被视为null:

it has been assigned the constant NULL. 它已被赋予常量NULL。

it has not been set to any value yet. 它尚未设定为任何值。

it has been unset(). 它一直未设置()。

<?php
if(is_null($obj)){
    echo 'is null';
}

While $obj is considered NULL because it hasn't been registered yet, it will still throw a Notice: undefined ... 虽然$obj被认为是NULL,因为它还没有被注册,但它仍然会抛出一个Notice: undefined ...

The correct use for your if statement should first check to see if the variable exists then check to see if it is an array. 正确使用if语句应首先检查变量是否存在,然后检查它是否是一个数组。

if (!isset($required) || !is_array($required)) {}

-or- -要么-

if (empty($required) || !is_array($required)) {}

The nice thing about is_array() is the fact that if $required IS NULL then it's not an array and will pass the if clause. 关于is_array()是,如果$required NULL,那么它不是一个数组,而是传递if子句。

isset() and empty() will both ensure that the variable exists. isset()empty()都将确保变量存在。 Failure to pass either of these methods will quit the if statement and no errors will be returned. 如果未通过其中任何一种方法,将退出if语句,并且不会返回任何错误。

I appreciate all the feedback, but somehow I think part of my question was not addressed. 我很感谢所有的反馈,但不知何故,我认为我的部分问题没有得到解决。 The fact that the variable is being tested with a Logical AND, means that both statements must be TRUE for the Exception in the if clause to run. 使用Logical AND测试变量的事实意味着要运行if子句中的Exception,两个语句都必须为TRUE。

But, I don't think the use of !is_null($required) is correct. 但是,我不认为使用!is_null($ required)是正确的。 The sample code from the book was testing for a variable to contain an array with one to many values. 本书中的示例代码测试变量以包含具有一对多值的数组。 Even if it has one value, the $required variable (for other reasons) still must be declared as an array with a single value. 即使它有一个值,$ required变量(由于其他原因)仍然必须声明为具有单个值的数组。 So, if $required hold's a value of (int) 5, an Exception should be thrown. 因此,如果$ required保持值为(int)5,则应抛出异常。 If the $required variable is not declared/instantiated, an Exception should be thrown. 如果未声明/实例化$ required变量,则应抛出异常。 If the $required variable holds NULL, an Exception should be thrown. 如果$ required变量保持NULL,则应抛出异常。 Here is where the logic of this sample code fails. 这是此示例代码的逻辑失败的地方。 Using the online php command line that @Calimero posted, this logic fails when $required is set to NULL. 使用@Calimero发布的在线php命令行,当$ required设置为NULL时,此逻辑失败。

Instead of using !is_null($required) , is_null($required) without the negation should have been used since is_null returns TRUE if the value of $required is indeed NULL. 如果$ required的值确实为NULL,则is_null返回TRUE,而不是使用!is_null($ required),is_null($ required)不应该使用否定。 So, if you negate is_null() when the $required value happens to be NULL, that part of the logical AND operation becomes FALSE, therefore the Exception never gets run at all because the logical AND operation requires both statements to be TRUE for the logic to jump into the curly braces. 因此,如果在$ required值恰好为NULL时否定is_null(),则逻辑AND操作的那一部分变为FALSE,因此异常永远不会运行,因为逻辑AND操作要求两个语句对于逻辑都为TRUE跳进花括号。 Which is precisely what the if clause was supposed to catch in the first place. 这正是if条款应该首先捕获的内容。 The sample code was supposed to catch the $required variable not being set and not being of an array type. 示例代码应该捕获未设置的$ required变量,而不是数组类型。

And as I mentioned in a comment, isset() probably wasn't used because isset will return TRUE even if the $required variable is an empty string, a string with a space, an empty array, etc. 正如我在评论中提到的,isset()可能没有被使用,因为即使$ required变量是空字符串,带空格的字符串,空数组等,isset也会返回TRUE。

Someone please confirm I'm not talking stupid. 有人请确认我说的不是傻瓜。 LOL. 大声笑。

Take a look at this: ( http://3v4l.org/QpVXq ) 看看这个:( http://3v4l.org/QpVXq

* *Is_null** The is_null is php construct language and can be used to test whether a variable is set to null. * * Is_null ** is_null是php构造语言,可用于测试变量是否设置为null。

   $myVariable = null;
    is_null($myVariable); // this returns true,because it is set to null

$myVariable = 10;
is_null($myVariable); // this returns false, cuz it is assigned to value


If the variable does not exist is_null will also return true, 
but with an error notice as it is not supposed
 to be used with uninitialized variables.

 //returns true and also notice with (undefined variable notice) 

    is_null($myNewVariable);// true (undefined variable notice) 

If the code is part of a function, it could be that the function allows for null values, but if the value is passed and it's not an array, then an exception must be thrown. 如果代码是函数的一部分,那么函数可能允许空值,但如果传递的值并且它不是数组,则必须抛出异常。 If the code is not part of a function, it may be just demonstrating how to detect if a value is an array only if it isn't null, thus allowing null values. 如果代码不是函数的一部分,它可能只是演示如何仅在值不为空时检测值是否为数组,从而允许空值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM