PHP - How can I treat the fifth item in a set of results differently?

时间:2016-10-20 18:58:11

标签: php

So I have been googling a lot today to find a way to do this. I could not find a way. My question is how do i do this :

if result is a total of 5 then do 'this' for the first 4 results then do something else for the 5th result and above.

I am searing for doing this with php but i have a small feeling this is unpossible.

1 个答案:

答案 0 :(得分:2)

If you are using a for loop then you can try something like this

<?php
for ($i = 1; $i <= 5; $i++) {
    if($i ==5){
        //code for value of 5
    }else{
        //code for other values
    }
}

Edit: I'm not quite certain what you're asking as I can't see your code, is this what you meant? (the == was replaced with <=)

<?php

    $arr = [1,2,3,4,5,6];

    for ($i = 1; $i <= 5; $i++) {
        if($i <=5){
            //code for value of 5
            echo $arr[$i];
        }else{
            //code for other values
        }
    }