Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用IF语句映射订单状态_Php_Wordpress_If Statement_Woocommerce_Orders - Fatal编程技术网

Php 使用IF语句映射订单状态

Php 使用IF语句映射订单状态,php,wordpress,if-statement,woocommerce,orders,Php,Wordpress,If Statement,Woocommerce,Orders,在Woocommerce中,我尝试使用以下代码映射订单状态: function my_map_status ($status) { if ($status == "wc-processing") { return "WAA"; } else { return $status; } if ($status == "wc-cancelled") { return "WAC"; } else { r

在Woocommerce中,我尝试使用以下代码映射订单状态:

function my_map_status ($status) {
    if ($status == "wc-processing") {
        return "WAA";
    } else {
        return $status;
    }
    if ($status == "wc-cancelled") {
        return "WAC";
    } else {
        return $status;
    }
}
但只有第一种方法有效


我怎样才能使它同时适用于这两种情况呢?

第一个
IF
ELSE
语句考虑了所有的可能性。改为使用
IF
ELSEIF
ELSE
结构:

function my_map_status ($status) {
    if ($status == "wc-processing") {
        return "WAA";
    } elseif ($status == "wc-cancelled") {
        return "WAC";
    } else {
        return $status;
    }
}

它应该更好地工作。

如果它没有超出第一个
if
的原因是它有一个只返回的
else
,因此,如果你从逻辑上考虑,你会发现如果$status不是wc processing,那么返回(并退出函数)——换句话说,它永远不会超出第一个
if

相反,您可能会考虑使用<代码>开关/CASE < /C>,这比多个代码>更容易阅读.I/EL如果 s,像这样:

switch ( $status ) {
    case "wc-processing":
        return "WAA";
        break;
    case "wc-cancelled":
        return "WAC";
        break;
    default:
        return $status;
}
(如果您想知道
中断
——虽然在这种情况下并非绝对必要(因为函数将随
返回退出
),但最好记住每次编写
开关
结构时都使用它。下页有更多信息。)

进一步阅读: