Thursday, 13 December 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...