-2

I have the following:

$first = $index % 2 == 0 ? 'partial_one' : 'partial_two';
$last = $index % 2 == 0 ? 'partial_two' : 'partial_one';

I have a feeling there is a shorter way to do this. Anyone?

Such Much Code
  • 787
  • 1
  • 9
  • 26

1 Answers1

3

This is not shorter, but in my opinion more readable:

if ($index % 2 == 0) {
    $first = 'partial_one';
    $last = 'partial_two';
}
else {
    $first = 'partial_two';
    $last = 'partial_one';
}
Andreas
  • 2,821
  • 25
  • 30
  • Thanks! I went for this answer in the end, and also Paul's suggestion to get rid of == 0. Thanks for your responses, everyone! – Such Much Code Apr 12 '16 at 18:07