PHP函数返回字符串

The*_*Kid 9 php string function

我是PHP的新手.我有一个检查价格成本的功能.我想从这个函数返回变量以便全局使用:

<?
function getDeliveryPrice($qew){
    if ($qew=="1"){
        $deliveryPrice="60";
    } else {
        $deliveryPrice="20";
    }
    return $deliveryPrice;                          
}
// Assuming these two next lines are on external pages..
getDeliveryPrice(12);
echo $deliveryPrice; // It should return 20

?>
Run Code Online (Sandbox Code Playgroud)

Jon*_*Jon 14

您只需将返回值存储在变量中:

$deliveryPrice = getDeliveryPrice(12);
echo $deliveryPrice; // will print 20
Run Code Online (Sandbox Code Playgroud)

$deliveryPrice上面的变量是与函数内部不同的变量$deliveryPrice.由于范围可变,后者在函数外部不可见.

  • @TheBlackBenzKid:有,但这是您可以使用的最糟糕的做法之一*而且*它只是“全局或无”-如果调用代码不在全球范围。接受我的建议,不要去那里。函数也有可能通过引用接受“输出参数”,但同样:这不是应该如何使用该功能。 (2认同)