Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
If I have 30 session variables and I want to do an if statement to only 10 of them.
if (empty($_SESSION['XYZ'])) { $_SESSION['XYZ'] = 0; }
what should I do? Do I call an if statement after every session variables that i needed for?
Consider putting the names of the keys in an array and then looping through with foreach.
$keys = array('x','y','z'); foreach ($keys as $key) { if (empty($_SESSION[$key]) { // do stuff } }
An advantage is you can quickly add or remove keys to check.
Add a comment
$vars = array('XYZ','ABC','DEF'); foreach($vars as $i){ if(empty($_SESSION[$i])) $_SESSION[$i] = 0; }
[edit] I like how we have the same answer 4 times now, and counting!
If they are in series, that is, from 0 to 29 then you can use a loop to traverse them!
Something like so?
$keys = array("A", "B", "C", "D", "E", "F"); foreach ($keys as $key) { if (empty($_SESSION[$key])) { $_SESSION[$key] = 0; } }
You could list the keys you want to zero-out in an array and set them as follows:
$keys = array('XYZ','FOO','BAR'); foreach ($keys as $key) { if (empty($_SESSION[$key])) { $_SESSION[$key] = 0; } }
Required, but never shown
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.
Explore related questions
See similar questions with these tags.