str_replace中的函数无法正常工作

str_replace中的函数无法正常工作

问题描述:

Please see my code below, I am trying to put a function containing an if statement within str_replace, however, it is not injecting it into the script, thank you in advance for your help. Basically I need to replace [price] in the string with this if statement.

function price(){
    if ($_SESSION['vat'] == "ex"){ 
       echo('£'.$row_products['price'].'Ex. VAT');
    } ?>
    <?php if ($_SESSION['vat'] == "inc"){     
        $total_price = $row_products['price'] *= (1 + $VATrate / 100);
        echo('&pound;');
        printf("%.2f", $total_price); 
        echo('Inc. VAT');
    }  

}

$main_description = str_replace('[price]', price(), $row_products['description']);

Not tested but something along the lines of this should do the trick:

function price() {
    if ($_SESSION['vat'] == "ex"){ 
        return '&pound;'.$row_products['price'].'Ex. VAT';
    } 

    if ($_SESSION['vat'] == "inc"){ 

        $total_price = $row_products['price'] *= (1 + $VATrate / 100);
        return sprintf("%s %.2f %s", "&pound;", $total_price, 'Inc. VAT'); 
    }  
}

You can't echo result inside a function you need to return a value. Your function should look something like this:

function price() {
    $result = ''
    if ($_SESSION['vat'] == "ex"){ 
       $result .= '&pound;'.$row_products['price'].'Ex. VAT' . "
"
    }
    if ($_SESSION['vat'] == "inc") {
        $total_price = $row_products['price'] *= (1 + $VATrate / 100);
        $result .= '&pound;';
        $result .= sprintf("%.2f", $total_price); 
        $result .= 'Inc. VAT';
    }  
    return $result;
}

You concatenate the string an then return it at the end.

The commentary is in the comments:

// When you create functions do not assume that variables
// will just magically be available inside it. If you need
// to use some variable inside a function then define them
// as arguments.
function price($price, $vat) {

    // Never use a variable or array index without
    // first making sure that it exists! You will
    // get error messages if they do not exist.
    if ( ! isset($_SESSION['vat'])) {
        trigger_error('The session variable "vat" is not set');
    }
    // Since you use 'ex' and 'inc' to figure out if the
    // VAT is included or not, what happens it the value
    // is neither of those? Maybe $_SESSION['vat'] should be
    // a boolean value instead? (I.e. true or false, as
    // opposed to one of N values.)
    $vatInfo = 'VAT may or may not be included...';

    // VAT excluded
    if ($_SESSION['vat'] === 'ex') {
        $vatInfo = 'Ex. VAT';
    }
    // VAT included
    elseif ($_SESSION['vat'] === 'inc') {
        $vatInfo = 'Inc. VAT';
        // The value of the $price variable will not be changed
        // after this so we can just overwrite it here.
        $price = ($price * (1 + $vat / 100));
    }
    // When it comes to money it is nice to format it so
    // that it looks good to the user. number_format() was
    // made for this.
    $price = number_format($price, 2);

    // And *return* it, as opposed to echo'ing it.
    return "&pound;{$price} {$vatInfo}";
}
// Pass in the price and VAT rate as arguments so that
// they can  be used inside the function.
$price = price($row_products['price'], $VATrate);
$main_description = str_replace('[price]', $price, $row_products['description']);