You are on page 1of 1

Display:

Five #1 is at position - 4
Five #2 is at position - 14
Five #3 is at position - 24
Five #4 is at position - 34

That conditional statement in our while loop may look a little intimidating, but not if you break it down.

• $offset = strpos($numberedString, "5", $offset + 1) - This is our conditional statement for our PHP While
Loop. If this ever is false the while loop will stop running. This conditional statement always runs before
each pass through the while loop.
• strpos($numberedString, "5", $offset + 1) - This is the same code we used in a previous example. We
are going to search our string numberedString for the number 5 and use the last match's value (stored
in $offset) + 1 to skip over the last match. The first $offset we use has a value of 0, so that we start at
the beginning of the string.
• $offset = strpos(... We are going to store the location returned by strpos into $offset so that we can
skip this match the next time the while loop runs through the code. If strpos ever fails to find a match
then this will be set to false making our while loop stop executing.

You might also like