Easiest way to alternate row colors in PHP/HTML?

前端 未结 19 1907
無奈伤痛
無奈伤痛 2020-12-01 03:41

Here\'s a PHP example of mine. Can anyone find a shorter/easier way to do this?


    
19条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 04:14

    On a side noe, to alternate between two values a and b, a nice way of doing it in a loop is this:

    x = a;
    while ( true ) {
        x = a + b - x;
    }
    

    You can also do this without addition and subtraction:

    x = a ^ b ^ x;
    

    where ^ is the XOR operation.

    If you just want to alternate between 0 and 1, you can do this:

    x = 0;
    while ( true ) {
        x = !x;
    }
    

    You could of course use x as an index of colors, CSS style classes and so on.

提交回复
热议问题