tags:

views:

95

answers:

6

I'm trying to figure out someone else's code and have come across this piece of code:

            $html = '<div class="event">' . "\n";

        if (get ( 'Event_Image' ))
        {
        $html .= '<a href="' . get ( 'Event_Image' ) . '">'
        . '<img src="' . pt () . '?src=' . get ( 'Event_Image' ) . '&amp;w=100" alt="' . get_the_title () . '" />'
        . '</a><br />' . "\n";
        }

        $html .= '<a href="' . get_permalink ( $eventId ) . '">' . //  title="Permanent Link to ' . get_the_title_attribute() . '"
get_the_title () . '</a><br />' . "\n";

        if (get ( 'Event_Time' ))
        {
            $html .= get ( 'Event_Time' ) . '<br />' . "\n";
        }

        if (get ( 'Store_Location' ))
        {
            $html .= get ( 'Store_Location' );
        }

        $html .= '</div><!-- event -->' . "\n";

        $eventsArr [$dateArr] [$eventId] = $html;
    }

My question: What does the .= mean? Does it add to the variable (in this case $html)?

+4  A: 

Yes. See http://www.php.net/manual/en/language.operators.string.php.

MvanGeest
+3  A: 

It means concatenate/append the value on the right hand to the value stored in the variable:

$a  = 'str';
$a .= 'ing';
echo $a; // string
Gordon
Second time today that my answer outranked and was earlier than the others, but was not chosen. How do you do it? (Sorry for polluting SO, but I can't put a comment on meta.)
MvanGeest
@MvanGeest Cant tell for sure, but from my experience an answer is more likely to be accepted if it provides at least one example and/or gives a proper link text. Add some meat to it.
Gordon
@Mvan Geest - Your answer becomes irrelevant if the URL changes and it requires clicking over to another site. Additionally, manual pages are often not easy to read, so an answer in your own words, or even if you copy and paste only the relevant areas from the linked page would be better.
Peter Ajtai
@Gordon: Thanks for the tips. @Peter Ajtai: According to good web practice, an URL should never change (**Cool URI's don't change** says the W3C over at http://www.w3.org/Provider/Style/URI), and it has an additional advantage of being updated with never versions of PHP (though the `.=` operator will hopefully *never* be different from what it is now). I get what you're saying, I just didn't want to "shamelessly plagiarize" the manual by copying both the text and the example. I'll try to find a better balance tomorrow (capped for now...).
MvanGeest
+1  A: 

It means concatinate equals. So

$var = 'foo';
$var .= 'bar';

echo $var;
// output is 'foobar'
Cags
A: 

it appends what comes after the equals to the variable before the .=

Peter Hanneman
appends not prepends
Gordon
I agree - just misspoke :)
Peter Hanneman
+1  A: 

It is concatenate, then assign.

Same as:

$html = $html . $someString;
Daniel A. White
+2  A: 

Yes, you got it right, here is an example:

$str  = 'Hello ';
$str .= 'World';
echo $str;

Result:

Hello World
Sarfraz