Amit L.
Amit L.

Reputation: 111

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.

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. To my understanding, is_null() returns TRUE if the variable is not set or is NULL. 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? The only way an exception is thrown is if (true && true) is satisfied.

Upvotes: 3

Views: 3326

Answers (4)

Kyle
Kyle

Reputation: 4014

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.

Upvotes: 0

Amit L.
Amit L.

Reputation: 111

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.

But, I don't think the use of !is_null($required) is correct. 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. So, if $required hold's a value of (int) 5, an Exception should be thrown. If the $required variable is not declared/instantiated, an Exception should be thrown. If the $required variable holds NULL, an Exception should be thrown. 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.

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. 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. Which is precisely what the if clause was supposed to catch in the first place. The sample code was supposed to catch the $required variable not being set and not being of an array type.

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.

Someone please confirm I'm not talking stupid. LOL.

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

Upvotes: 1

Mubo
Mubo

Reputation: 1070

**Is_null** The is_null is php construct language and can be used to test whether a variable is set to 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) 

Upvotes: 0

Samuel Cook
Samuel Cook

Reputation: 16828

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

A variable is considered to be null if:

it has been assigned the constant 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 ...

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 (!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.

isset() and empty() will both ensure that the variable exists. Failure to pass either of these methods will quit the if statement and no errors will be returned.

Upvotes: 3

Related Questions