(php) strpos offset in while loop -
i'm studying php reading tutorial in website(http://www.tizag.com/) below excerpt tutorial.
<?php $numberedstring="1234567890123456789012345678901234567890"; $offset=0; $fivecounter=0; echo $numberedstring; if(strpos($numberedstring, "5")==0){ $fivecounter++; echo "<br />five #$fivecounter @ position - 0"; } while($offset=strpos($numberedstring, "5", $offset+1)){ $fivecounter++; echo "<br />five #$fivecounter @ position - $offset"; }?>
i don't understand how offset changes. shouldn't code in while() true? seems designating offset. understand first offset 0. when goes to
while($offset=strpos($numberedstring, "5", $offset+1)
for first time, offset changes 1 due '$offset+1'. then, think
strops($numberedstring, "5", $offset+1)
becomes 4.
and guess 4 becomes offset again, , start loop again, how come codes in while() can designate else? shouldn't true output something?
hanky's answer correct, i'll fill in few of details since, judging fact working on tutorial trying learn php.
at beginning of script, variable $offset
set 0.the first time while loop runs (provided 5 not identified in position 1 in if statement proceeding) offset incremented 1 (the $offset+1
in the strpos
function in while loop) doing incrementing $offset variable above. strpos
function return either position of next 5 in string (and change value of $offset
variable once again, time newest position of 5) or return false , in effect end loop execution.
Comments
Post a Comment