'If / Else in HTML within a return of PHP function

I am trying to use an if/else statement within an HTML structure of a return in a PHP function:

function price_vat() {
    global $product;
    $condition = $product->get_attribute( 'pa_condition' );
    
    return'
    <div class="condition-container">
        <div class="condition-wrapper">
        <?php if($condition = "New"){ ?>
            <div id="content-banned">New</div>
        <?php } else { ?>
            <div id="content-not-banned">Used</div>
        <?php } ?>
        </div>
    </div>
    ';
}

I am sure it is just a syntax issue here, does anyone have any ideas? Also tried with a tertiary operator but couldn't get it to work. Any input is appreciated.

php


Solution 1:[1]

You can't do like that. You try to put php code in a php code with a return of a function. Try that (I don't test it) :

function price_vat() {
    global $product;
    $condition = $product->get_attribute( 'pa_condition' );
    $html = '<div class="condition-container"><div class="condition-wrapper">';

    if($condition == "New")
      $html .= '<div id="content-banned">New</div>';
    else
      $html .= '<div id="content-not-banned">Used</div>';

    $html .= '</div></div>';
    return $html;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 svgta