Re: php: assignment operator
| From: | Analysis & Solutions | Date: | Wed, 07 Aug 2002 15:24:49 +0000 |
| Subject: | Re: php: assignment operator | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-111522@lists.php.net to get a copy of this message | ||
On Tue, Aug 06, 2002 at 10:57:49PM -0500, Anthony Ritter wrote:
>
> I tried an example with the number 3 replacing the number 1
> $image_src=$image_dir. "/" .substr($counter, $i, 3) . ".jpg";
That's not what I'm talking about. I said "strlen($counter)", which
determines the number of times the for loop itterates. Read the Control
Structures section of the manual to see how for loops work.
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\">";
}
The short section of script you showed us makes me think you're working on
a script that's going to display a graphical counter on your page. The
reason that you're not seeing a difference between .= and = is because
your counter is still below 10, so the length of $counter is 1, thus the
loop only executes once. Bump the counter up to 10 and see what
happens... The loop will execute twice and you'll get two image tags in
the $image_tag_src variable.
> However, in examples such as:
> $a="This is a";
> $a.="test";
> echo $a;
> // displays This is a test
> There is a variable that has previously existed with a value
>
> In the script that I supplied, the variable:
> $image_tag_src
> did not hold any value until the line with the .= operator
That's because the script is poorly programmed so it has a vulnerability,
as mentioned in my earlier post. I assume you took this directly
from the book you mentioned: "Beginning PHP4" by Wrox. Looks like we
should steer people clear of that book in the future.
Regardless, if a variable is set ahead of time or not, the value on the
right gets appended to the existing value of the variable. When the
variable isn't set yet, the value starting value is null and the new value
gets tacked onto it.
Enjoy,
--Dan
--
PHP classes that make web design easier
SQL Solution | Layout Solution | Form Solution
sqlsolution.info | layoutsolution.info | formsolution.info
T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y
4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409