"P1 attacks P2" and "P2 attacks P1"
This is a refactoring of the original code. It picks the starting player and then uses a simple toggle ($currentPlayer = ($currentPlayer == 1) ? 2 : 1;) to determine which player goes next for 5 rounds. 

$currentPlayer = 1;

if ($player2speed &gt; $player1speed) {

    $currentPlayer = 2;

}

$count = 0;

while ($count &lt; 5) {

    echo $currentPlayer . " turns&lt;br/&gt;";

    $currentPlayer = ($currentPlayer == 1) ? 2 : 1;

    $count ++;
}


Note, the current code fails if both player's speeds are the same.