1

Why do this :

$myarray = array();
$_SESSION['myarray'] = serialize($myarray);

$myarray = unserialize($_SESSION['myarray']);
if ($myarray == null) {
    print_r($myarray);
    print("<br>no data");
    exit;
}

gives this as a result :

Array ( )
no data

How my var can contains an array and be tested TRUE to null ?

0

2 Answers 2

3

Type juggling is responsible. An empty array and null both equal false. Because you use the == comparison operator, type is not compared they are considered equal. If you use the === comparison operator then an empty array is not equal to null as the if statement evaluates to false.

Demo

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

1 Comment

Thank you for these fine references
2

Because of loose comparison that you are using ( == ), $myarray == null is the same as $myarray == [], loose comparison means that only values are compared and both these values are empty. To compare the type as well as the value use strict comparison with ===.

Type comparison tables, Comparison Operators

2 Comments

Isn't this exactly what I wrote?
@JohnConde, I'm sorry, I did not read the other answers while writing my own, but if it is such a problem I will delete my answer gladly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.