Jump to content

Shortcutting Logical Operators


Recommended Posts

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')) {

Link to comment
https://forums.phpfreaks.com/topic/235880-shortcutting-logical-operators/
Share on other sites

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'))) {

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

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...

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.

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...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.