Re: php: assignment operator
| From: | Anthony Ritter | Date: | Wed, 07 Aug 2002 03:57:49 +0000 |
| Subject: | Re: php: assignment operator | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-111465@lists.php.net to get a copy of this message | ||
> That's the case if strlen($counter) is 1. If it were more, you'd see
> multiple image tags in there.
>
> --Dan
................................
Thanks Dan.
I tried an example with the number 3 replacing the number 1
.......................
$image_src=$image_dir. "/" .substr($counter, $i, 3) . ".jpg";
..........................
and the script works o.k. so I couldn't see the difference.
Also, I understand that the difference is that .= appends onto the value of
the variable
whereas = replaces the value of the 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
so I am still baffled.
Many thanks.
TR
...............................................
Analysis & Solutions <danielc@analysisandsolutions.com> wrote in message
news:20020807004145.GA29909@panix.com...
> On Tue, Aug 06, 2002 at 07:54:47PM -0500, Anthony Ritter wrote:
> >
> > 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\">"; //
assignment
> > operator .=
> > }
>
> See
> http://www.php.net/manual/en/language.operators.assignment.php.
>
> .= appends the text to the right of the operator to the end of the
> variable on the left side of the operator.
>
> Therefore, $image_tag_src will be one long string containing several "<img
> src..." in it, one for each time the loop executes.
>
> Be careful. Before using this operator, make sure you manually set the
> variable to something, such as:
>
> $image_tag_src = '';
>
> before starting in order to avoid people hacking your code by putting
> nasty text in the $image_tag_src variable.
>
>
> > If I was to assign the html string to the variable $image_tag_src
without
> > the .= and insert a =, the script continues to run fine since
$image_src
> > still contiunes to hold the value of
> > $image_dir. "/" .substr($counter, $i, 1) . ".jpg";
>
> That's the case if strlen($counter) is 1. If it were more, you'd see
> multiple image tags in there.
>
> --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