Php str_replace内的功能无法正常工作

Php str_replace内的功能无法正常工作,php,Php,请看下面的代码,我试图在str_replace中放入一个包含if语句的函数,但是,它并没有将其注入脚本,提前感谢您的帮助。基本上,我需要用if语句替换字符串中的[price] function price(){ if ($_SESSION['vat'] == "ex"){ echo('&pound;'.$row_products['price'].'Ex. VAT'); } ?> <?php if ($_SESSION['vat'] =

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

function price(){
    if ($_SESSION['vat'] == "ex"){ 
       echo('&pound;'.$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']);
函数价格(){
如果($_会话['vat']==“ex”){
echo(“£;”.$row_产品[“价格”]。“增值税除外”);
} ?>

未经测试,但类似于此的东西应该可以做到:

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'); 
    }  
}

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

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;
}

将字符串连接起来,然后在末尾返回。

注释位于注释中:

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

你的函数
echo
s它的结果。你需要
返回它们。清理你的代码让我们阅读。现在它很乱。为什么你有
?>谢谢你的帮助,尽管在函数内部,变量是空的,在函数外部它工作正常-有什么想法吗?再次感谢你的帮助。你需要将其分配到函数中。函数不知道函数外部声明的变量,除非它们被传递到函数中(函数价格($productPrice){..}),或者该变量被声明为全局变量,即全局$row_products;-请参阅