1

I have used is_array many times. The variable is an array, see my code, but when i use is_array function it returned false

Any Ideas?

Here is my code.

public function updateCategories($array = null)
{   
    echo gettype($array); // echos array
    if($array = null)
    {    
        return false;
    }    
    if(!is_array($array))
    {      
        echo "false"; // echos false
        return false;
    }

    foreach($array as $key => $val)
    {
        foreach($val as $Property => $Value)
        {
            if(!$this->updateCategoryProperty($key, $Property, $Value))
            {
                return false;
            }
        }
    }
}

2 Answers 2

13

Your if($array = null) is not comparing, but assigning.

Sign up to request clarification or add additional context in comments.

3 Comments

WOOOOW i cant believe i missed that, i went over my code many times. Thank you very much.
Exactly. Your editor should have pointed that for you. I use Netbeans, and it gives me a warning when I do assignments inside if statements. You should try it.
This is common mistake to avoid it write the if statement like this if(null == $array) so if you missed '=' php will throw syntax error
1

Additionally to Russel Dias answer: always write your "null-checks" in this way:

if (null == $my_var)

now, if missing one "=", this will always get a real error, because you cannot assign something to null.

1 Comment

Sorry for the necro, but why would my "null-checks" always be written this way? Why not empty( $my_var ) || !is_array( $my_var ), leaving the obvious fact that it's shorter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.