Hobbyist_PHPer Posted May 9, 2011 Share Posted May 9, 2011 In php is there a way to shortcut logical operators... example: if ($row['CustomerSaas'] == 'Yes' && ($row['CustomerSaasPaidStatus'] == 'Trial' || $row['CustomerSaasPaidStatus'] == 'Trial Ended')) { to if ($row['CustomerSaas'] == 'Yes' && ($row['CustomerSaasPaidStatus'] == 'Trial' || 'Trial Ended')) { Quote Link to comment https://forums.phpfreaks.com/topic/235880-shortcutting-logical-operators/ Share on other sites More sharing options...
fugix Posted May 9, 2011 Share Posted May 9, 2011 yep that will work Quote Link to comment https://forums.phpfreaks.com/topic/235880-shortcutting-logical-operators/#findComment-1212554 Share on other sites More sharing options...
Hobbyist_PHPer Posted May 9, 2011 Author Share Posted May 9, 2011 Really, because I can't it to work... If I have Trial Ended in the field, it doesn't work... but if I have Trial in the field, it does work... Even tried this... if ($row['CustomerSaas'] == 'Yes' && ($row['CustomerSaasPaidStatus'] == ('Trial' || 'Trial Ended'))) { Quote Link to comment https://forums.phpfreaks.com/topic/235880-shortcutting-logical-operators/#findComment-1212555 Share on other sites More sharing options...
PFMaBiSmAd Posted May 9, 2011 Share Posted May 9, 2011 There is no such thing for logic operators (you are logically or'ing the string 'Trial' with 'Trial Ended' which produces a bool(true) if I am not mistaken and then testing if your variable is equal to a bool(true).) However, if you have one variable that you want to test if it is one of several values, you would make an array of the values and use in_array Quote Link to comment https://forums.phpfreaks.com/topic/235880-shortcutting-logical-operators/#findComment-1212559 Share on other sites More sharing options...
Hobbyist_PHPer Posted May 9, 2011 Author Share Posted May 9, 2011 There is no such thing for logic operators (you are logically or'ing the string 'Trial' with 'Trial Ended') However, if you have one variable that you want to test if it is one of several values, you would make an array of the values and use in_array That makes sense... So basically it's just as well to write it out unless you have a lot of values to check against, in which case you would put them in an array and check against them that way... Quote Link to comment https://forums.phpfreaks.com/topic/235880-shortcutting-logical-operators/#findComment-1212564 Share on other sites More sharing options...
PFMaBiSmAd Posted May 9, 2011 Share Posted May 9, 2011 You could also put the array inline in the statement for a small number of choices - if ($row['CustomerSaas'] == 'Yes' && in_array($row['CustomerSaasPaidStatus'], array('Trial','Trial Ended'))) { Given that these values are probably coming from a query, you can probably accomplish the same by putting the conditions into a WHERE clause in the query so that only rows that match those conditions are returned by the query. Quote Link to comment https://forums.phpfreaks.com/topic/235880-shortcutting-logical-operators/#findComment-1212566 Share on other sites More sharing options...
Hobbyist_PHPer Posted May 9, 2011 Author Share Posted May 9, 2011 You could also put the array inline in the statement for a small number of choices - if ($row['CustomerSaas'] == 'Yes' && in_array($row['CustomerSaasPaidStatus'], array('Trial','Trial Ended'))) { Given that these values are probably coming from a query, you can probably accomplish the same by putting the conditions into a WHERE clause in the query so that only rows that match those conditions are returned by the query. I like that, that's some really nice coding... Thanks again for the help... Note taken on query... Quote Link to comment https://forums.phpfreaks.com/topic/235880-shortcutting-logical-operators/#findComment-1212569 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.