How to make a php function working inside an echo?

2904 views php
3

I tried adding HTML inside a PHP echo, and it works.

But the echo is not working if with a PHP function,

The original code:

if( ! function_exists("rframework_cart_button") ){
    function rtframework_cart_button(){

        $show_cart = get_theme_mod( RT_THEMESLUG.'_top_shortcut_buttons_cart' ) ? true : false ;

        if( ! class_exists('Woocommerce') || ! $show_cart ){
            return;
        }

        global $woocommerce;

        echo '<li class="cart"><a href="#" class="cart-menu-button"><span class="icon-shopping-bag"><sub class="number empty">'. $woocommerce->cart->cart_contents_count .'</sub></span></a></li>'."\n";

    }
}

The PHP code I want to put inside or after the echo is:

<li class="heart"><a href="#" class="heart-button"><span class="icon-new-heart-1"><sub class="number empty">0</sub></span></a></li>

Can anyone help?

answered question

Maybe your question is not clear but can you not add another echo after the first with your HTML string or even after your function call, eg rtframework_cart_button(); echo '<li...';

YES, I also tried another echo under the echo, it works if is HTML, but not working if with PHP function, that's why I need help.

Please edit your question and explain what you mean with examples.

2 Answers

3

echo is like constructor display the string,So if you add any php function in echo it would be treated as string. http://php.net/manual/en/function.echo.php

posted this
13

with echo, you need to change all the double quotes inside the html stuff to single quotes and keep double quotes outside. Try this:

  echo "<li class='cart'><a href='#' class='cart-menu-button'><span class='icon-shopping-bag'><sub class='number empty'>{ $woocommerce->cart->cart_contents_count }</sub></span></a></li>'.'\n";

posted this

Have an answer?

JD

Please login first before posting an answer.