【问题标题】:multiple return in function函数中的多次返回
【发布时间】:2014-06-16 20:27:55
【问题描述】:

有问题的 php 新手。这是计算值是否在 ex 之间的函数。 0,00 和 1.500,00 那么折扣数是 8

function wpsc_cart_discount_number(){
    global $wpsc_cart;
    if   ($total >= 0000 && $total < 1500) {
        return $discount->wpsc_cart_discount_number = '8';
    }   if ($total >= 1500 && $total < 3000) {
        return $discount->wpsc_cart_discount_number = '11';
    }   if ($total >= 3000 && $total < 7000) {
        return $discount->wpsc_cart_discount_number = '13';
    }  if ($total >= 7000 && $total < 10000) {
        return $discount->wpsc_cart_discount_number = '16';
    }  if ($total >= 10000) {
        return $discount->wpsc_cart_discount_number = '18';
    }
}

当我调用这个函数时,它只返回“8”的值,而不是其他这里有什么问题?

请帮忙

谢谢

【问题讨论】:

  • 为了让论坛上的每个人都清楚,如果您在问题的标题和标签中包含更多信息可能会很有用,例如“PHP:函数问题中的多个返回值”或其他内容:)
  • 你可能想在这个语句中使用 elseif 而不是 6 个 if 语句:P 但我怀疑这就是问题的解决方案
  • 试过了,但它总是返回与上面“8”相同的值

标签: php return return-value


【解决方案1】:

基于事实,您的函数中没有本地 $total 变量,它是未定义的。通过 PHP 灵活的错误处理,这只是一个通知,而不是错误。与整数比较时,未定义的值被视为0,因此第一个条件将始终为真。

【讨论】:

  • Total 未预定义,它是在添加到购物车过程中计算的,wpsc_cart_discount_number 我使用另一种 php 表单创建 pdf 发票,无论我的购物车中的总数是 700 欧元还是 7000 欧元,它都会继续显示pdf 折扣编号 8。这是来自 pdf 生成器发票 $params[]= array('unique_name'=&gt;'$params[]= array('unique_name'=&gt;'cart_total','value'=&gt;wpsc_cart_total()); $params[]= array('unique_name'=&gt;'popust','value'=&gt;wpsc_cart_popust()); 的部分
  • $total 是一个局部变量,我在上面的函数中没有看到对它的赋值。
【解决方案2】:

只是看看它我猜你没有 $total 被定义。你能放一些调试代码看看你的 $total 是否有值吗?

或者发布更多代码以及 $total 的定义位置。

【讨论】:

    猜你喜欢
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2014-04-23
    相关资源
    最近更新 更多