makamo66 Posted October 17, 2019 Share Posted October 17, 2019 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(). Quote Link to comment https://forums.phpfreaks.com/topic/309384-null-session-array-condition-not-met-until-page-is-refreshed/ Share on other sites More sharing options...
Barand Posted October 17, 2019 Share Posted October 17, 2019 (edited) 6 minutes ago, makamo66 said: array(0) { } That is telling you it is an array with zero elements, for example $a = [1,3,9]; var_dump($a); //--> array(3) { [0]=> int(1) [1]=> int(3) [2]=> int(9) } Edited October 17, 2019 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/309384-null-session-array-condition-not-met-until-page-is-refreshed/#findComment-1570764 Share on other sites More sharing options...
ginerjm Posted October 17, 2019 Share Posted October 17, 2019 Uh.... When you unset the variable any test for it not being empty or not being null is going to fail so no echo. If you had php error checking turned on you would probably be seeing a warning/notice concerning that session var. Quote Link to comment https://forums.phpfreaks.com/topic/309384-null-session-array-condition-not-met-until-page-is-refreshed/#findComment-1570778 Share on other sites More sharing options...
makamo66 Posted October 17, 2019 Author Share Posted October 17, 2019 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/309384-null-session-array-condition-not-met-until-page-is-refreshed/#findComment-1570795 Share on other sites More sharing options...
requinix Posted October 18, 2019 Share Posted October 18, 2019 It won't print out anything because the code is invalid. We can't help you troubleshoot your code if you don't provide us with your real code. Quote Link to comment https://forums.phpfreaks.com/topic/309384-null-session-array-condition-not-met-until-page-is-refreshed/#findComment-1570798 Share on other sites More sharing options...
makamo66 Posted October 18, 2019 Author Share Posted October 18, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/309384-null-session-array-condition-not-met-until-page-is-refreshed/#findComment-1570799 Share on other sites More sharing options...
Barand Posted October 18, 2019 Share Posted October 18, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/309384-null-session-array-condition-not-met-until-page-is-refreshed/#findComment-1570800 Share on other sites More sharing options...
makamo66 Posted October 18, 2019 Author Share Posted October 18, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/309384-null-session-array-condition-not-met-until-page-is-refreshed/#findComment-1570801 Share on other sites More sharing options...
mac_gyver Posted October 18, 2019 Share Posted October 18, 2019 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 - 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. 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. 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. Quote Link to comment https://forums.phpfreaks.com/topic/309384-null-session-array-condition-not-met-until-page-is-refreshed/#findComment-1570802 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.