Jump to content

Null session array condition not met until page is refreshed


makamo66

Recommended Posts

When I use the following code, it doesn't print out "test" until after I refresh the page.

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

Link to comment
Share on other sites

EDIT:

When I use the following code, it doesn't print out "else if test" until after I refresh the page.

unset($_SESSION["product"]);

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

Link to comment
Share on other sites

EDIT:

When I use the following code, it doesn't print out "else test" until after I refresh the page.

unset($_SESSION["product"]);

if ( !empty($_SESSION["product"]) && ($_SESSION["product"] != NULL)) {
echo "if test";
} else {
echo "else test";
}
EDITED POST TO CHANGE ELSE IF INTO ELSE

Link to comment
Share on other sites

20 hours ago, ginerjm said:

If you had php error checking turned on you would probably be seeing a warning/notice concerning that session var.

A variable is "empty" if it doesn't exist  so the code will avoid "undefined index" notices. It's also empty if it is null, so the second condition is superfluous.

Adding var_dump($_SESSION['product']) gives the expected error but not that line.

However, contrary to makamo66's experience, it does print "else test" so the problem is probably caused elsewhere in the code.

Link to comment
Share on other sites

36 minutes ago, makamo66 said:

it doesn't print out "else test" until after I refresh the page.

since the code is un-setting the variable and will always output 'else test', if you are not seeing the output, ether -

  1. your code is redirecting all over or using ajax to display content and you are not actually 'on' the page where this code is at, but the refresh does 'goto' the page where this code is at.
  2. you have some conditional logic that is preventing the posted code from being executed, either explicate logic or an exit/die statement, which the refresh doesn't trigger, and so the posted code gets executed.
  3. some html is being conditionally output prior to the posted code and the output from the posted code is hidden in the 'view source' of the page, but, again, the refresh doesn't trigger the prior output, and so you see the output from the posted code. 

since you have repeated the unset() statement with the variations of the code, i/we assume that is actually part of this code. do you understand what the unset() statement does and what affect it will have on the conditional logic test(s) you have posted?

as stated on one of the other help forums, all you are showing/describing are snippets of code and symptoms. if you just post all the relevant code for the page(s) involved with this problem, someone can probably solve this in a couple of minutes.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.