Jump to content

Question about logical operators


Jiokah

Recommended Posts

Good suggestions, but I think they over-complicate things a bit more. I was hoping to solve this within a single expression, ie "If variable equals 3 or 6, return true" instead of "If variable equals 3 or variable equals 6, return true".

 

I thought of this example:

 

if ($someVariable == (5 || 6)) {//Do Something}

 

...but both 5 and 6 are true, therefore the expression would always return true.

 

The actual expression I'm hoping to simplify is the following:

 

(strlen($someNumber) > 16 || strlen($someNumber) < 6) //Returns true or false

Link to comment
Share on other sites

if ($someVariable == (5 || 6)) {//Do Something}

 

If you look at how it parses this, (5 || 6) is checked first. Is 5 (true) || 6(true)? Yep! Then it will check if $someVariable is true, since that is what was returned inside the parenthesis. The reason this happens is because the logical operators are comparing two conditions and the only way to have the conditions varry is by using the comparison operators.

 

In short, you can't. If all you are trying to do is reduce the number of times you are calling the strlen function, just use:

$someNumberLen = strlen($someNumber);
($someNumberLen > 16 || $someNumberLen < 6) //Returns true or false

 

Good luck!

 

Link to comment
Share on other sites

I knew (5 || 6) would return true regardless, as I explained in my previous post, I had simply included that example to give people a better understanding of what I'm getting at.

 

I think your right lemmin, it doesn't look possible. Oh well. I don't really NEED to do this per se, but I think it'd just make my code look neater.

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.