如何将各种IF条件合并为最终结果

时间:2016-01-02 14:26:36

标签: php if-statement

我正在构建一个处理URL的脚本,并将根据各种条件对其进行修改。

我不确定这样做的最佳做法是什么;例如,现在,在"合作伙伴A"部分,如果类别与" premiuim"不同,我的final_url变量将保持为空。我想我可以制定一个条件,检查它是否为空,在这种情况下它应该适用$url3的值,但它不够聪明:

<?php

$url = "http://www.default.com"; // default URL

$url = "something"; // this part will come from a database

$id="something"; // some value to insert later into URL

// the following rules will be followed according to various parameters not defined in this sample code

if($partner == "A") { // Partner A rule:

    $url2 = str_replace('777', '999', $url); // Inital Change

    $url3 = str_replace('?location', '?id=' . $id . '&location', $url2); // Add id, anytime ?location shows up

    if($category == "premium") { // premium category rules:

        $re = "/(?<=TID%3D)\\d+/";  // String to Replace in URL (digits)
        $newtid = "4000"; // Default New TID

                if($special == "yes") { $newtid = "8000";} // special TID value

        $final_url = preg_replace($re, $newtid, $url3); // replace the TID

    }

};


if($partner == "B") { // Partner B rule:

$final_url = str_replace('status=1', 'config=' . $id . '&status=1', $url); //Add id, anytime status=1 exists in url
};

?>  

<p>Final URL: <?php echo $final_url; ?></p>

此外,我不确定整个结构是否是最佳结构。

修改

更新代码:

if($category == 'A') {
    // A category rules:
    $url2 = str_replace('aaa', 'bbb', $url); // Global rule #1
    $url3 = str_replace('?url', '?id=' . $id . '&url', $url2); // Glbal rule #2
    $final_url = $url3; // Final URL unless following cases occur

    if($offer == 'Special') {
        //category is A and offer is special

        $re = "/(?<=TID%3D)\\d+/";  // replace rule for current case
        $tid = "123"; // Default New TID value for current case
        $final_url = preg_replace($re, $tid, $url3); // Final URL for this case unless following case occur

        if ($discount == '10') {
            // category is A, offer is special, and Discount is 10

            $re = "/(?<=TID%3D)\\d+/";  // replace rule for current case
            $tid = "999"; // Special TID value for current case
            $final_url = preg_replace($re, $itd, $url3); // Final URL for this case
        }
    }
} 

2 个答案:

答案 0 :(得分:1)

这应该简化代码:

str_replace

你的正则表达式是一样的,所以你不需要定义它两次。 shape = input("Please enter your choice of shape? ") nthTime = ["second","third","fourth","fifth","sixth"] while shape.lower() not in ["octagon","heptagon","hexagon"] : print("Please select a shape from the list!") shape = input("Pick a shape, for the " + nthTime + " time! ") 也可以使用多个搜索字段并替换值。

答案 1 :(得分:0)

解决此问题的一种有效方法可能是switch/case声明:

switch (TRUE) {

case ($partner == 'A') :
/* rules; */

if ($category == 'premium') {
/* rules; */
}

else {
/* rules; */
}

/* rules; */

break;

case ($partner == 'B') :
/* rules; */
break;
}

=====

原创建议:

switch (TRUE) {

case (($partner == 'A') && ($category == 'premium')) :
/* rules; */
break;

case ($partner == 'A') :
/* rules; */
break;

case ($partner == 'B') :
/* rules; */
break;
}

=====

修改建议:

switch (TRUE) {

case (($partner == 'A') && ($category == 'premium')) :
/* rules; */

case ($partner == 'A') :
/* rules; */
break;

case ($partner == 'B') :
/* rules; */
break;
}