Thursday, December 13, 2018

php - Multiple condition IF statement



I've got an if statement with multiple conditions that I just can't seem to get right



if(((ISSET($_SESSION['status'])) && ($_SESSION['username']=="qqqqq")) ||     
((ISSET($_SESSION['status'])) && ($_SESSION['company']=="wwwwww")) ||
((ISSET($_SESSION['status'])) && ($_SESSION['company']=="ffffffff")) ||
((ISSET($_SESSION['status'])) && ($_SESSION['company']=="ggggggg")) ||

((ISSET($_SESSION['status'])) && ($_SESSION['company']=="hhhhhhh")) ||
($_SESSION['LOGINTYPE']=="ADMIN")) {


Its supposed to return true if the status is set as well as either one of those company names OTHERWISE if the logintype is admin it should also return true regardless of company or status


Answer



What about this :



if (
(isset($_SESSION['status']) && $_SESSION['username']=="qqqqq")

|| (isset($_SESSION['status']) && in_array($_SESSION['company'], array('wwwwww', 'ffffffff', 'ggggggg', 'hhhhhhh')))
|| $_SESSION['LOGINTYPE']=="ADMIN"
)


Rewriting the condition to try to make it a bit more easier to read might help :




  • One possible case per line :


    • status set and username OK

    • status set and company OK

    • admin


  • Using in_array instead of several tests on company -- I think it's easier to understand


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...