Null array condition not met until page is refreshed

When I use the following code, it doesn’t print out “test” until after I refresh the page. I WROTE THIS INCORRECTLY PREVIOUSLY AND AM NOW EDITING. I DON’T WANT IT TO PRINT OUT TESTS. IT SHOULD PRINT OUT NOTHING AT ALL.

unset($_SESSION["product"]);

if ( !empty($_SESSION["product"]) && ($_SESSION["product"] != NULL)) {
echo "test";
}

When I use:

var_dump($_SESSION["product"]);

It shows:

array(0) { } 

I’m wondering it that’s the problem. That it’s array(0) when it should be array().

Since you haven’t shown where and how you are setting $_SESSION[‘product’], we can only guess as to the reason for the current symptom. The exact posted code will never display the ‘test’ output since it is always un-setting the session variable before the logic test. It would require having all the relevant code to be able to help.

As stated on two other help forums, that I know of, that’s what the var_dump() of an empty array is.

What is the overall problem you are having that you are trying to solve? The things you have posted are just symptoms and out of context snippets of code, and don’t tell use what you are really doing that isn’t working.

The code didn’t actually make the call to unset the variables until after the not empty conditional was asked. I was misled because var_dump still printed out an empty session array. Once I changed the order of my code, it worked.

This is how I read it. If that isn’t the structure of the file, then it would be hard to pinpoint why it wasn’t working.

Sponsor our Newsletter | Privacy Policy | Terms of Service