1

Possible Duplicate:
php if statement with multiple conditions

I have this code:

if(x==1 || x==2 || x==3 || x==4 )

Is there anyway to simply it shorter? For example:

if(x==1||2||3||4 )

Meaning for the statement to be true if X= 1,2,3 OR 4?

Thank you in advance.

Edit: (Thanks for all the replies, here is some clarification)

I have a while loop but only want specific database entries to be called. My code is current

<?php while(the_repeater_field('team_members','options') && get_sub_field('member_sort') == 1 : ?>
 <div class="one_fourth">
        <img src="<?php the_sub_field('image'); ?>" alt="" />
        <p>
          <?php the_sub_field('info'); ?>
            <br />
            <a href="mailto:<?php the_sub_field('email'); ?>"><?php the_sub_field('email'); ?></a>
        </p>
 </div>
<?php endwhile; ?>

Right now it works perfectly with == 1 , but I also want to show 2,3,4 as well. I am just not sure how I should've put the code to execute 1||2||3||4

Update 2:

Okay so I used the following code, but I am guessing I am going about it the wrong way. The following code only showed the record that was equal to 1.. but not the records equal to 2,3,4... I am guess because the while loop is only running once since the statement becomes true instantly.

  <?php while(the_repeater_field('team_members','options') && in_array(get_sub_field('member_sort'),array(1,2,3,4))): ?>
                        <div class="one_fourth">
                            <img src="<?php the_sub_field('image'); ?>" alt="" />
                            <p>
                                <?php the_sub_field('info'); ?>
                                <br />
                                <a href="mailto:<?php the_sub_field('email'); ?>"><?php the_sub_field('email'); ?></a>
                            </p>
                        </div>
                    <?php endwhile; ?>
Community
  • 1
  • 1
Damainman
  • 515
  • 9
  • 21
  • 1
    What's wrong with the longer code? What problem are you trying to solve? Would a [`switch` statement](http://rosettacode.org/wiki/Conditional_structures#switch_12) work in your case? – sarnold May 29 '12 at 01:04

4 Answers4

5
if (in_array($x, array(1,2,3,4))

or, even:

if (in_array($x, range(1, 4)))

OK, this question has evolved quite a bit but I think the problem is now that you want to loop over all values but only do stuff on certain conditions. You can use the continue statement to abort the current iteration and move on to the next immediately.

<?php while (the_repeater_field('team_members','options')) : ?>
    <?php if (!in_array(get_sub_field('member_sort'), array(1,2,3,4))) continue; ?>

    ... do stuff

 <?php endwhile; ?>
Okonomiyaki3000
  • 3,628
  • 23
  • 23
  • in_array is slow, however - see: us3.php.net/manual/en/function.in-array.php#93880 – damianb May 29 '12 at 01:06
  • 1
    Well i think the range thing is pointless... could do `$x>=1 && $x<=4 && is_int($x)` – d_inevitable May 29 '12 at 01:11
  • 1
    @damianb how many millions of times do you need to run this line of code? If your answer is < 10, I doubt you'll notice. – Okonomiyaki3000 May 29 '12 at 01:15
  • @d_inevitable True, range's use is pretty limited but in some cases it can be very useful. For example, you can also do `range('a', 'd')` if you need to match alphabetic characters or `range(2, 10, 2)` to match only even numbers 2-10. – Okonomiyaki3000 May 29 '12 at 01:18
  • This may work, I just want to make sure it is the most efficient way of going about it. I updated my question to explain in more detail. I also plan to run similar code that does the opposite... meaning !=1||2||3||4 – Damainman May 29 '12 at 01:28
  • 1
    Then, of course, you would use `!in_array(...)`. But what kind of efficiency are you talking about? Efficiency in terms of number of keystrokes used or speed of executing your code? To execute most quickly, you should use something like this: `if(x==1 || x==2 || x==3 || x==4 )` but put the comparisons in order of most to least likely to be true. But please beware of premature optimization. – Okonomiyaki3000 May 29 '12 at 01:37
  • @Okonomiyaki3000 thanks for the reply. Would I put that if statement inside the while loop I posted above? I guess what is confusing me is I know how to do x==1||x==3||x==3 but not sure how to do it with the && operator in the mix for y==Z && x==1||x==3||x==3 .... would that be the order I put the code in? – Damainman May 29 '12 at 04:30
  • 1
    @Damainman if your condition is starting to look pretty complex and you can't easily understand it at a glance, you'd better break it up with (). But what do you really mean? `(y==Z && x==1) || x==3 || ...` is different from `y==Z && (x==1 || x==2 || ...)` If it's looking ugly even after that, you might consider breaking into multiple `if` statements. Don't get hung up on ***efficiency***. Write your code in a way you can understand. – Okonomiyaki3000 May 29 '12 at 04:50
  • @Okonomiyaki3000 thanks again for the reply... I believe the following worked y==Z && (x==1 || x==2 || ...)... But i think I went about it the wrong way because I was expecting the while loop to run to show the records for each one 1,2,3,4 but it stops at 1 since the statement is true. I updated my question. – Damainman May 29 '12 at 05:08
  • Without knowing what `the_repeater_field` and `get_sub_field` should be returning, I don't think I can be of much help. – Okonomiyaki3000 May 29 '12 at 05:28
  • Thanks for the reply once again lol. I guess my error is in the while loop. I just need it to run for each true statement but I guess that might be a different question :(. – Damainman May 29 '12 at 05:37
  • Actually to simplify it.. I just need the while statement to run each time the statement is true... so if x==1 is true then run and repeat, and if x==2 is also true.. then run and repeat to see if x==3 is true. and run. – Damainman May 29 '12 at 05:42
  • I think I see the problem. `While` will loop until the condition is `false` then it will exit. I think you just want to skip the current iteration if it's false and move on to the next, right? – Okonomiyaki3000 May 29 '12 at 06:17
  • Yes :) that is exactly what I am trying to do. Basically I have 20 database entries only 4 of them have values in the 'member_sort' field(values are 1,2,3,4). I want to print out the database entry with 'member_sort' == 1, 'member_sort' == 2, etc ... the code "get_sub_field('member_sort')" just grabs the 'member_sort' value from the database entry and it is how I get 1,2,3,4. It is not an array. – Damainman May 29 '12 at 06:44
  • Why not just add `WHERE member_sort IN(1,2,3,4)` to your query so you only get the rows you want in the first place? – Okonomiyaki3000 May 29 '12 at 07:38
  • It is a function from a wordpress plugin ( http://www.advancedcustomfields.com/docs/field-types/repeater/ )and I am not sure how to use an sql query to work with it. My knowledge is sort of limited lol. However your solution worked perfectly! It displays the 4 I need. Now to try to figure out how to sort it in order by 1,2,3,4. Thank you soo much!! – Damainman May 29 '12 at 08:02
1

It depends on how many you have, and on whether there may be other conditions, but either a switch:

switch(x) {
    case 1:
    case 2:
    case 3:
    case 4:
        break;
    default:
        // Do stuff
}

Or an array, especially for many items:

if(!in_array(x, array(1, 2, 3, 4)) {
    // Do stuff
}
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • Thank you for taking time to answer. I am not sure how to implement switch cases to work with my code above or know whether or not that is the route I should be going. – Damainman May 29 '12 at 04:43
  • @Damainman: To implement it with NOT, you mean? Here, I've updated it. – Ry- May 29 '12 at 13:19
1

If you are using PHP 5.4+, you could do this:

if (in_array($x, [1,2,3,4]))

Otherwise, it's:

if (in_array($x, array(1,2,3,4)))
Ry-
  • 218,210
  • 55
  • 464
  • 476
d_inevitable
  • 4,381
  • 2
  • 29
  • 48
  • in_array is slow however and should be used sparingly; see: us3.php.net/manual/en/function.in-array.php#93880 – damianb May 29 '12 at 01:07
  • @damianb I think that comment is missleading, because it takes a lot of time to build the associative index. Plus it does not compare against an or statement. But I do agree that in_array could be a tick slower. But only a tiny bit. – d_inevitable May 29 '12 at 01:13
  • More or less, with simple vars, yes; when you start getting into objects, it starts making a huge difference. – damianb May 29 '12 at 01:16
  • if I used the second option in PHP 5.4+ is there any performance difference than if I used the first option? Just curious if it's different or the same. – Damainman May 29 '12 at 01:32
  • @Damainman well technically its just the same thing. Any performance difference would be strictly due to the inner working of the two versions. In either case it would be minimal. – d_inevitable May 29 '12 at 01:36
0

There is no way. Only by using arrays.

Hatem
  • 493
  • 2
  • 5
  • 12