0% found this document useful (0 votes)
39 views

PHP Tizag Tutorial-73

Five is found at positions -4, -14, -24, and -34 in the numbered string. The document then explains a PHP while loop used to find all positions of the number "5" in the string. It uses the strpos() function to search for "5", starting from the last match position plus one to skip already found matches. The match position is stored in the $offset variable and used on subsequent loops. If strpos() fails to find another match, it will return false and end the while loop.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

PHP Tizag Tutorial-73

Five is found at positions -4, -14, -24, and -34 in the numbered string. The document then explains a PHP while loop used to find all positions of the number "5" in the string. It uses the strpos() function to search for "5", starting from the last match position plus one to skip already found matches. The match position is stored in the $offset variable and used on subsequent loops. If strpos() fails to find another match, it will return false and end the while loop.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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