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
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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