RE: [PHP] php: assignment operator

From: Date: Wed, 07 Aug 2002 14:46:25 +0000
Subject: RE: [PHP] php: assignment operator
Groups: php.general 
Request: Send a blank email to php-general+get-111508@lists.php.net to get a copy of this message
> -----Original Message----- > From: Anthony Ritter [mailto:tony@gonefishingguideservice.com] > Sent: 07 August 2002 16:23 > > Exactly. I don't understand. :-0 No, on the contrary, you understand perfectly! > O.K. The script is a counter. > > My question was about the assigment operator > > .= > > within the following script taken from page 330 of WROX > "Beginning PHP 4". > > I didn't understand why there was a > .= > > as opposed to a > = > > and my guess is that by using the .= operator You don't need to guess -- just "dry run" the script with various values of $counter, and you'll see exactly what's happening. > > > > the output would echo ONE placeholder for the IMG SRC from 0 > > > through 9 - however when the 10th time the page was > loaded it would > still > > > be in the *same* IMG SRC holder and there never be a > SECOND IMG SRC > > > holder - to the right - (10 through 99), a third to the > right of the > second > > > (100 through 999), etc. > > as opposed to using a > = operator > > which would only have one SRC placeholder if the page was > refreshed the > tenth time around. Yes, exactly -- but without the information about where exactly $counter was coming from, I was being very confused about the reference to page refreshes -- and, in a way, it's irrelevant, except as a clue to what likely values of $counter are! > The complete script: > ........................................ > > <? > $counter_file="./counter.txt"; > $image_dir= "./images"; > if(!($fp=fopen($counter_file, "r")))die("Couldn't open the file"); > $counter=(int)fread($fp, 20); > fclose($fp); > $counter++; > for($i=0; $i<strlen($counter); $i++) > { > $image_src=$image_dir. "/" .substr($counter, $i, 1) . ".jpg"; > $image_tag_src.="<IMG SRC=\"$image_src\" BORDER=\"0\">"; > } > echo "You are visitor $image_tag_src"; > $fp=fopen($counter_file, "w"); > fwrite($fp,$counter); > fclose($fp); > ?> > .................................... OK, so if we dry run this with, say, $counter==9 on reaching the for loop: - strlen($counter) == 1 - loop executes with $i==0 (<1) - $image_tag_src (currently empty) has an image tag for "9.jpg" appended to its right-hand end - $i increments to 1 - loop finishes because $i==1 and $image_tag_src contains 1 image tag for "9.jpg". In this instance, there is no difference between the results for = and .= assignments. Now, if $counter==10: - strlen($counter) == 2 - loop executes with $i==0 (<2) - $image_tag_src (currently empty) has an image tag for "1.jpg" appended to its right-hand end - $i increments to 1 - loop executes with $i==1 (<2) - $image_tag_src now has an image tag for "0.jpg" appended to its right-hand end - $i increments to 2 - loop finishes because $i==2 Now, $image_tag_src has image tags for *two* images, "1.jpg" and "0.jpg". Here, if the assignment had been the simple assignment =, the second iteration of the loop would have overwritten $image_tag_src instead of appending it, and you would have ended up with just the image tag for "0.jpg". This kind of manual dry run can be invaluable for understanding/debugging code, and in the absence of a proper debugger with proper step-trace facilities is an exceedingly useful tool!! Cheers! Mike --------------------------------------------------------------------- Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning & Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: m.ford@lmu.ac.uk Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211

« previous php.general (#111508) next »