Q:

What is the correct method to wrap a heap of code into a variable

I'm working with a shopping cart script that doesn't let me add php to the template files. The method used is to create $GLOBALS within the class files and call them from the tpl files. What I need to do is the following.
$GLOBALS['DiscountedPrice'] needs to be equal to that chunk of code that follows. I've tried a few syntax options to try and wrap it up, but it breaks because the same syntax is used inside the chunk of code. 
$GLOBALS['DiscountedPrice'] =  $query = mysql_query("
             SELECT *
             FROM isc_customer_group_discounts
             WHERE `catorprodid` = '%%GLOBAL_ProductId%%'
             AND `customergroupid` = 1
             AND `discounttype` = 'PRODUCT'
             ");
$discounted_price = '';
while($proddisc = mysql_fetch_array($query)) {
    $discperc = $proddisc['4'];
    $discmeth = $proddisc['6'];
    if($discmeth = 'percent') {
        $discperc = $proddisc['4']/100;
        $discounted_price = $row['prodprice'] * $discperc;
        $GLOBALS['DiscountedPrice'] = $discounted_price;
    }elseif ($discmeth = 'price') {
        $discounted_price = $row['prodprice'] - $discperc;
        $GLOBALS['DiscountedPrice'] = $discounted_price;
    }elseif ($discmeth = 'fixed') {
        $discounted_price = $discperc;
        $GLOBALS['DiscountedPrice'] = $discounted_price;
    }};

A:

$GLOBALS['DiscountedPrice'] .= $discounted_price;

Use a dot to concatenate the string as the loop goes round.  (no space between . and =)
EDIT.
Possibly I have not fully understood you question, but if you want to continually join strings this is what you'd do.
$discounted_price = ''; // you start with an empty string
while($proddisc = mysql_fetch_array($query)) {
$discperc = $proddisc['4'];
$discmeth = $proddisc['6'];
if($discmeth = 'percent') {
    $discperc = $proddisc['4']/100;
    $discounted_price = $row['prodprice'] * $discperc;
// and now you concat (join) it to the new value
    $GLOBALS['DiscountedPrice'] .= $discounted_price;
}elseif ($discmeth = 'price') {
    $discounted_price = $row['prodprice'] - $discperc;
    $GLOBALS['DiscountedPrice'] .= $discounted_price;
}elseif ($discmeth = 'fixed') {
    $discounted_price = $discperc;
    $GLOBALS['DiscountedPrice'] .= $discounted_price;
}};

