Jiokah Posted December 14, 2007 Share Posted December 14, 2007 I have a simple question. How would I write the following expression while using the variable only once? if ($someVariable == 3 || $someVariable == 6) { //Do Something } Thanks! Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 14, 2007 Share Posted December 14, 2007 Put the values you want to test for in an array and then use the in_array() function in the if() statement - http://php.net/in_array Quote Link to comment Share on other sites More sharing options...
thebadbad Posted December 14, 2007 Share Posted December 14, 2007 You could use switch, or an array: <?php switch ($someVariable) { case 3: case 6: // do something } ?> <?php if (in_array($someVariable, array(3, 6))) { // do something } ?> Quote Link to comment Share on other sites More sharing options...
Jiokah Posted December 14, 2007 Author Share Posted December 14, 2007 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 Quote Link to comment Share on other sites More sharing options...
lemmin Posted December 14, 2007 Share Posted December 14, 2007 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! Quote Link to comment Share on other sites More sharing options...
Jiokah Posted December 14, 2007 Author Share Posted December 14, 2007 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. Quote Link to comment Share on other sites More sharing options...
marcus Posted December 14, 2007 Share Posted December 14, 2007 why not just something like: $range = range(6,16); $variable = "hello!"; if(in_array(strlen($variable),$range)){ // OMG }else { // FAIL } Quote Link to comment Share on other sites More sharing options...
Jiokah Posted December 14, 2007 Author Share Posted December 14, 2007 Because that's not accomplishing what I want in one expression. I'm aware of all the hundreds of ways I can accomplish this with minimal code, I'm a fairly advanced programmer. My question was simply regarding syntax, not method. Thanks though Quote Link to comment 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.