PHP函数返回字符串 [英] PHP Function to return string

查看:144
本文介绍了PHP函数返回字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PHP相当陌生。我有一个检查价格成本的功能。我想从这个函数中返回这个变量来全局使用:

I am fairly new to PHP. I have a function which checks the cost of price. I want to return the variable from this function to be used globally:

<?
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

?>


推荐答案

您应该将返回值存储在一个变量中:

You should simply store the return value in a variable:

$deliveryPrice = getDeliveryPrice(12);
echo $deliveryPrice; // will print 20

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

The $deliveryPrice variable above is a different variable than the $deliveryPrice inside the function. The latter is not visible outside the function because of variable scope.

这篇关于PHP函数返回字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆