Syntax for echoing html and PHP

I need to add some PHP and HTML to a action hook in my WordPress site
I can not work out how to add the php so that Sub total text is translatable _e(‘Sub-total’, ‘wpsc’)
That line throws a syntax error in my editor

add_action('wpsc_before_form_of_shopping_cart', 'add_my_totals');

function add_my_totals() {
	$my_new_totals = '<table  class="wpsc_checkout_table table-4">
	<tr class="total_price">
   <td  class="wpsc_totals">
   <?php _e('Sub-total', 'wpsc'); ?>:
   </td>
   <td  class="wpsc_totals">
   <?php echo wpsc_cart_total_widget( false, false ,false ); ?>
   </td>
   </tr>
</table>';

   echo "$my_new_totals";
   }

Since it is part of a string, you will want to concatenate the function to your text.

add_action('wpsc_before_form_of_shopping_cart', 'add_my_totals');

function add_my_totals() {
	$my_new_totals = '<table  class="wpsc_checkout_table table-4">
	<tr class="total_price">
   <td  class="wpsc_totals">' .
   _e('Sub-total', 'wpsc') .
   '</td>
   <td  class="wpsc_totals">' .
   wpsc_cart_total_widget( false, false ,false ) .
   '</td>
   </tr>
</table>';

   echo "$my_new_totals";
   }

The alternative would be to exit out of PHO for the HTML instead of addigning it to a variable and then echoing it:

add_action('wpsc_before_form_of_shopping_cart', 'add_my_totals');

function add_my_totals() {
    ?><table  class="wpsc_checkout_table table-4">
    <tr class="total_price">
   <td  class="wpsc_totals">
   <?php _e('Sub-total', 'wpsc'); ?>:
   </td>
   <td  class="wpsc_totals">
   <?php echo wpsc_cart_total_widget( false, false ,false ); ?>
   </td>
   </tr>
</table><?php
   }

Another alternative would be to use cpradio’s code but replace the $my_new_totals = with echo and drop the echo “$my_new_totals”;

Thanks to both of you! Very helpful. I could use a little clarification. The use of a period when the code is being echoed as a string by being in single quotes, switch between HTML and PHP. Is that a fair description?
I think that felgall’s version is clearer to read fro a novice like me. Any reason one is better than the other?
I guess cpradio’s version does not require me to go in and out of php tags within my functions file, I can just open one for the whole file

Now I am really confused. It seemed to me I applied the method properly in this code

add_action('wpsc_before_form_of_shopping_cart', 'add_my_totals');

function add_my_totals() {
	$my_new_totals = '<table  class="wpsc_checkout_table table-10">
 <tr class="total_price my-totals">
      <td class="wpsc_totals my-totals-pad">' .
      _e('Total Price including Shipping and Tax:', 'wpsc') .
      '</td>
      <td class="wpsc_totals">
         <span id="checkout_total" class="pricedisplay checkout-total">' . wpsc_cart_total(); '</span>
      </td>
   </tr>
   </table>';
   echo $my_new_totals;
   }

But the resulting HTML from the page source is this with the text outside the table completely!

Total Price including Shipping and Tax:
<table class="wpsc_checkout_table table-10">
    <tbody>
        <tr class="total_price my-totals">
            <td class="wpsc_totals my-totals-pad"></td>
            <td class="wpsc_totals"><span class="pricedisplay checkout-total" id="checkout_total"><span class="pricedisplay">$32.48</span></span></td>
        </tr>
    </tbody>
</table>

I did get it to work using felgall’s method


add_action('wpsc_before_form_of_shopping_cart', 'add_my_totals');

function add_my_totals() {
	?><table  class="wpsc_checkout_table table-10">
 <tr class="total_price my-totals">
      <td class="wpsc_totals my-totals-pad">
     <?php _e("Total Price including Shipping and Tax", "wpsc") ?>
      </td>
      <td class="wpsc_totals">
         <span id="checkout_total" class="pricedisplay checkout-total"><?php wpsc_cart_total(); ?></span>
      </td>
   </tr>
   </table>
   <?php echo $my_new_totals;
   }

?>

Just to explain why my method did not work and @felgall ; did.

I missed the fact that _e() was performing an echo to write out the translation, and because of that, it would produce the HTML result you received where the translation was outside of the table.

Thanks for clearing that up. I now happily have table of all price options, tax, shipping, coupons etc working

Good that I’ve actually been using for a while now. But now i got the clarity

Ugh… You should never have html in a function like that. Your function should deal with pure data. You should then be calling the function in your view (html) script and using the data returned to create the view/html.

If you hard code html into functions like that, you’ll make your site virtually impossible to change.

*Edit: I’m assuming wordpress doesn’t expect you to do that? I’ve never coded in wordpress before. It’s a horrible way to write code, because you end up with a design that’s nigh on impossible to change. Don’t do it!