0

请在下面查看我的代码,我正在尝试在 str_replace 中放置一个包含 if 语句的函数,但是,它没有将其注入脚本中,在此先感谢您的帮助。基本上我需要用这个 if 语句替换字符串中的 [price]。

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']);
4

3 回答 3

1

未经测试,但类似的东西应该可以解决问题:

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'); 
    }  
}
于 2013-06-21T13:55:29.533 回答
0

评论在评论中:

// 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']);
于 2013-06-21T14:23:16.963 回答
0

您不能在需要返回值的函数内回显结果。您的函数应如下所示:

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

您连接字符串然后在最后返回它。

于 2013-06-21T13:55:36.983 回答