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

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!

 

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.

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.