Why have to tagged this as C and C++? There is nothing here to do with either language, this is all PHP.
Why are you explicitly declaring an elseif statement when comparing speed? The only reason I can think of is if you are also going to compare for equality (going same speed). But you aren't, so an else statement would serve a better purpose here by comparing for both equality and less-than. Unless the initial "turn" variable values of 0 are supposed to cover this eventuality, but then they should be defined in a final else, otherwise this just looks odd. Besides, the while loop makes me think this is not the case.
if( $p1speed > $p2speed ) {
//change turns
} else {
//equivalent to if( $p2speed >= $p1speed )
}
//OR
if( $p1speed > $p2speed ) {
//change turns
} elseif( $p2speed > $p1speed ) {
//change turns
} else {
//default turns
}
Extending Player Pool
Some of the following might be beyond the scope of your project, but figured I'd mention it anyways, as its something to think about when developing a game, or any kind of application. Does my code allow for expansion? Is expansion necessary? Richard has already pointed out a better way to treat turns (+1), but the problem with his implementation is that it does not easily allow for more than two players. To accomplish this, you will need a counter, which you could reuse from the loop, and some sort of operation to reset the index when the counter has exceeded maximum number of players, I'm thinking modulo. So for example, using Richard's example we can easily determine who's turn it is on every iteration by using the following bit of code inside the loop.
$currentPlayer = $counter % $numberOfPlayers;
How then do we determine which player is first with a larger player pool? Which is second? We can't manually write each comparison. Well, we could, but that would be a bit much. Instead, if we get all the player speeds into an array, we can sort it and use the array indices to determine the order. Of course, that means we will have to tweak the above algorithm to use the array positions instead of a sequential incremental to ensure players going faster are not behind players going slower, but that's simple enough.
$speeds = array(
5,//player 1
3,//player 2
2//player 3
);
$numberOfPlayers = count( $speeds );
asort( $speeds );
//do other things and eventually start loop
$position = $counter % $numberOfPlayers;
$currentPlayer = $speeds[ $position ];
The problem with the above example is that the players are already in order, so it does not demonstrate this very well. But take my word for it, those numbers could easily be mixed up and still work. asort() is the key here. It sorts an array and keeps the array's indices in tact.
Loops
While loops are for testing a condition until otherwise (dis)proven. In other words, "is this statement TRUE?" Loop until it is. Typically this is done for things that already have this kind of boolean type associated with it and just need certain conditions to be met before it is passed. Such as when reading from a file. The problem with these kinds of loops is that it is very easy to accidentally create an infinite loop and crash the program. The need for a while loop should be rare.
For loops are for looping over a set of sequential data using an explicit counter.
Foreach loops are for looping over a set of non-sequential data using an internal (invisible) counter.
So, the question here is: Why are you recreating a for loop with a while loop? As miniBill pointed out, a for loop is better here. Hopefully the above explanations can help you figure out why. If your concern is speed, know that PHP has come a long way since the early days and for loops are now much better.
for( $i = 0; $i < 5; $i++ ) {
Cleaning Up
The main problem you seem to be having right now is that you are using procedural code, which, as you have noticed, results in a lot of bulky, repetitive code. The only cure for this is to start using a class or functions. I'd lean more towards a class here, but if you are unfamiliar with the concept, learning how to implement functions properly might be a better first step. Just for the sake of example, here is a turn toggle as I would write it in a class method. Of course, there is a better way to do this, as we have already discussed, but it serves to demonstrate the point quite well.
public function toggleTurns() {
$this->p1 = $this->p1 == 1 ? 2 : 1;
$this->p2 = $this->p2 == 1 ? 2 : 1;
}
//in some other method
for( $i = 0; $i < 5; $i++ ) {
if($p1==1) {
echo 'P1 turns <br>';
} else {
echo 'P2 turns <br>';
}
$this->toggleTurns();
}