Click to See Complete Forum and Search --> : what does .= mean?


hammerslane
12-09-2003, 09:51 AM
on php.net there are many examples which define variables by putting<? $variable .= "value"; ?>instead of<? $variable = "value"; ?>
the full stop before the equal sign always returns an error, and i can't figure out why php.net and others use it.

why am i asking such a simple question? because no search facilities search for .= ..... cheers :)

requestcode
12-09-2003, 10:07 AM
The ".=" means to append the value to what is already in the variable. It is a shortcut for $x=$x."test".

Pittimann
12-09-2003, 10:09 AM
Hi!

The dot "." is equivalent to "+". That means, if you have:
$variable .= "something"
and $variable already had a value before, "something" will be added to the former value of $variable. Example:
$variable = "abc" => value of $variable is "abc", then:
$variable .= "def" => value of $variable is "abcdef"...

Cheers - Pit

edit: Ooops - requestcode was already there!

hammerslane
12-09-2003, 10:09 AM
thanks very much.
my php book should have covered it... weird.

regards

pyro
12-09-2003, 10:11 AM
Also, the reason it didn't work for you was because you did not declare the variable.

pyro
12-09-2003, 10:12 AM
Originally posted by Pittimann
The dot "." is equivalent to "+".You must mean it is the equivalent of + in a different lanugage (JavaScript, perhaps), as the . and the + mean two different things in PHP...

hammerslane
12-09-2003, 10:14 AM
so it just bungs the value on the end of the currently defined variable then.... cheers guys. the world makes sense now.

many thanks

Pittimann
12-09-2003, 10:22 AM
Hi!
originally posted by pyro:
You must mean it is the equivalent of + in a different lanugage (JavaScript, perhaps), as the . and the + mean two different things in PHP...

I should have stated somethin like: The dot "." is equivalent to plus (because a new value is added to an old value) in order to avoid confusion.

Anyway, both the . and the + are "operators" for adding values. The . for strings and the + for numeric values.

Isn't both some sort of addition? :D

Cheers - Pit

hammerslane
12-09-2003, 10:25 AM
ah right... so when i see <? echo '<td>' . $variable . '</td>'; ?>what does that mean?

pyro
12-09-2003, 10:25 AM
In a way, I suppose. Just wanted to clear up any possible misconceptions that the . and the + opperators could be used interchangably. The . is the concatenation opperator, while the + is, of course, a mathimatical opperator.

pyro
12-09-2003, 10:27 AM
Again, just simple string concatenation. You are joining the <td> $variable and </td> into one string, so you can echo it out with one echo call.

hammerslane
12-09-2003, 10:29 AM
in my last example, why use the full stop though? why can't you just leave it out?

pyro
12-09-2003, 10:37 AM
If you don't want to concatenate the string before echoing it out, you'll want to take advantage of PHP's variable interpolation with double quotes. This is the equivalent of what you posted above:

<?PHP
echo "<td>$variable</td>"';
?>

echo can also take on additional paramaters, so you could use something like this, but I would not do so:

<?PHP
$foo = "foo";
$baz = "baz";
echo $foo, " bar ", $baz;
?>

As far as why you cannot leave the . out of the string to be echoed, when PHP sees the second ' in your string, it sees the end of the string, and expects you to either end the line with a ; or allows you to concatenate more content (stings/variables) to the current string with the concatenation opperator (.).

hammerslane
12-09-2003, 10:40 AM
many thanks again. this really is the best php forum on the internet imo... in devshed they just tell you to search google without even reading teh post

pyro
12-09-2003, 11:17 AM
Happy to help, and glad you like these forums... :)