如何在PHP中动态构建<<< EOD字符串[关闭]

I want to build my string dynamically depending on the results from the DB. So far I have created something like this:

        $feed = Some essential xml that needs to go here;
        for ($a = 0; $a < count($images); $a++)
        {
            if ($a == 0)
            {
                $image_type = "Main";
            }
            else
            {
                $image_type = "PT". $a;
            }
            $feed += <<<EOD
  <Message>
    <MessageID>1</MessageID>
    <OperationType>Update</OperationType>
    <ProductImage>
        <SKU>{$data['sku']}</SKU>
        <ImageType>{$image_type}</ImageType>
        <ImageLocation>{$images[$a]}</ImageLocation>
    </ProductImage>
  </Message>
EOD;

        }

        $feed += <<<EOD
</AmazonEnvelope>        
EOD;
echo $feed; 

This example of course returns nothing however I wanted to present my code in the way that I would like it to work. Do would I build the $feed string dynamically in this case?

In PHP string concatenation is done with the . operator, not the +. Therefore concatenate and assign is also .=, not +=. Fix that in both relevant lines and it'll work fine.

The underlying reason for the different operator here is that, unlike most other popular languages, PHP is weak-typed. The + operator is as such reserved for mathematical operations, so PHP can handle these 2 different lines 'correctly':

echo '123'+'123';    // Shows 246
echo '123'.'123';    // Shows 123123

Example.