Easiest way to alternate row colors in PHP/HTML?

前端 未结 19 1933
無奈伤痛
無奈伤痛 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:37

    In PHP I am using this code:

    function alternate($sEven = "even", $sOdd = "odd")
    {
        static $iCount;
        return ($iCount++ & 1) ? $sOdd :$sEven;
    }
    
    for($i = 0; $i< 5; $i++)
    echo alternate();
    
    
    /*output:
    
    even
    odd
    even
    odd
    even
    
    */
    

    Source: http://sklueh.de/2013/11/einfache-alternierung-mit-php/

提交回复
热议问题