删除“echo”并将其替换为“return”?

I have the following code in place, however, I have to strip away all the styling (any css) and have it return rather than echo, otherwise it messes up with some other code I'm using. The other code I'm using echo's a shortcode that outputs the following determined by if there is items in the cart or not.

Obviously I can't echo (on the echo shortcode end of things), so I must return. If the cart is empty, it returns just fine as per my commented code below. It is when the cart has items, I can't get it to return :( my attempt is below.

//Original code, if there's items

if(is_object($cart) && $cart->countItems()) {
  ?>
  <div id="Cart66scCartContents" style="float:right; text-align: right;">
    <a id="Cart66scCartLink" href='<?php echo get_permalink($cartPage->ID) ?>'>
    <span id="Cart66scCartCount"><?php echo $cart->countItems(); ?></span>
    <span id="Cart66scCartCountText"><?php echo $cart->countItems() > 1 ? ' items' : ' item' ?></span> 
    <span id="Cart66scCartCountDash">&ndash;</span>
    <!-- <span id="Cart66scCartPrice"><?php //echo CART66_CURRENCY_SYMBOL . 
      number_format($cart->getSubTotal() - $cart->getDiscountAmount(), 2); ?> -->
    </span></a>
    <a id="Cart66scViewCart" href='<?php echo get_permalink($cartPage->ID) ?>'>View Cart</a>
    <span id="Cart66scLinkSeparator"> | </span>
    <a id="Cart66scCheckout" href='<?php echo get_permalink($checkoutPage->ID) ?>'>Check out</a>
  </div>
  <?php
}
else {

//My code, if there's no items (which works perfectly as a return)

    $emptyMessage = isset( $attrs['empty_msg'] ) ? $attrs['empty_msg'] : 'Your cart is empty';
    return "<p id=\"Cart66scEmptyMessage\" style=\"float:right; text-align: right;\">" . $emptyMessage . "</p>";

}

My attempt on setting the "if there's items in the cart" to a return rather than echo..

if(is_object($cart) && $cart->countItems()) {      
  return "<a href='" . get_permalink($cartPage->ID) . "'>" . $cart->countItems(); . " " . $cart->countItems() > 1 ? ' items' : ' item' . "&ndash;" . number_format($cart->getSubTotal() - $cart->getDiscountAmount(), 2); . "</a> <a href='" . get_permalink($cartPage->ID) . "'>View Cart</a> | <a href='" . get_permalink($checkoutPage->ID) . "'>Check out</a>";
}
else {
    $emptyMessage = isset( $attrs['empty_msg'] ) ? $attrs['empty_msg'] : 'Your cart is empty';
    return "<p id=\"Cart66scEmptyMessage\" style=\"float:right; text-align: right;\">" . $emptyMessage . "</p>";

}

The text "1 item – View Cart | Check out" is not showing up with my attempt. What have I done wrong?

Thank you!!

You have a semicolon at . $cart->countItems();, remove that and you should be good to go

Maybe

$html = <<<EOD
        <tr>
          <td>TEST</td>
        </tr>
EOD;

Would help here?

Try to use HEREDOC

For example:

if (is_object($cart) && $cart->countItems()) {
    $items = $cart->countItems() > 1 ? ' items' : ' item';
    return <<<HTML
<div id="Cart66scCartContents" style="float:right; text-align: right;">
    <a id="Cart66scCartLink" href='{get_permalink($cartPage->ID)}'>
        <span id="Cart66scCartCount">{$cart->countItems()}</span>
        <span id="Cart66scCartCountText">{$items}</span>
        <span id="Cart66scCartCountDash">&ndash;</span>
        </span></a>
    <a id="Cart66scViewCart" href='{get_permalink($cartPage->ID)}'>View Cart</a>
    <span id="Cart66scLinkSeparator"> | </span>
    <a id="Cart66scCheckout" href='{get_permalink($checkoutPage->ID)}'>Check out</a>
</div>
HTML;
}
else {
    $emptyMessage = isset( $attrs['empty_msg'] ) ? $attrs['empty_msg'] : 'Your cart is empty';
    return "<p id=\"Cart66scEmptyMessage\" style=\"float:right; text-align: right;\">" . $emptyMessage . "</p>";

}

Declare the string variable and concatenate all the HTML you trying to echo in that string and return the string variable .

Example $string = '