2

我有一个代码来检查几个字段来执行某些代码,如下所示:

switch($tag1 || $tag2 || $tag3 || $tag4 ||$tag5){
            case "satay":
            $imgput = "/home/uploads/sandbox/jovine/Food/tags/satay/$img_name";
            break;

            case "digitalmarketing":
            $imgput = "/home/uploads/sandbox/jovine/Food/tags/interactive_marketing/$img_name";
            break;
            case "chillicrab":
            $imgput = "/home/uploads/sandbox/jovine/Food/tags/chilli_crab/$img_name";
            break;
            case "chickenrice":
            $imgput = "/home/uploads/sandbox/jovine/Food/tags/chicken_rice/$img_name";
            break;
            case "chendol":
            $imgput = "/home/uploads/sandbox/jovine/Food/tags/chendol/$img_name";
            break;
            }

但它不起作用。

任何人都可以帮忙吗?

4

2 回答 2

1

Switch只支持一个值。只有IF条件才能拥有ORAND条件。

$tags = get the tag value.
    switch($tags){
                case "satay":
                $imgput = "/home/uploads/sandbox/jovine/Food/tags/satay/$img_name";
                break;

                case "digitalmarketing":
                $imgput = "/home/uploads/sandbox/jovine/Food/tags/interactive_marketing/$img_name";
                break;
                case "chillicrab":
                $imgput = "/home/uploads/sandbox/jovine/Food/tags/chilli_crab/$img_name";
                break;
                case "chickenrice":
                $imgput = "/home/uploads/sandbox/jovine/Food/tags/chicken_rice/$img_name";
                break;
                case "chendol":
                $imgput = "/home/uploads/sandbox/jovine/Food/tags/chendol/$img_name";
                break;
                }
于 2013-09-09T04:08:41.493 回答
0

以下应该为您解决问题:

<?php

    $tag1='satay';
    $tag2='test2';
    $tag3='digitalmarketing';

    function isItThere($myTag)
    {
        switch($myTag)
        {
            case "satay":
                $imgput = "/home/uploads/sandbox/jovine/Food/tags/satay/$img_name";
                echo $imgput;
                break;
            case "digitalmarketing":
                $imgput = "/home/uploads/sandbox/jovine/Food/tags/interactive_marketing/$img_name";
                echo $imgput;
                break;
            // etc etc
        }
    }


    for($i=1;$i<4;$i++)
    {
        isItThere(${'tag'.$i});
    }

?>

我基本上设置了一个包含 switch 语句的小函数,并编写了一个简单的循环来测试变量。

正如我在评论中所说,您不能在 switch 语句中使用多个变量,但这将为您提供一个很好的干净的解决方法来执行相同的操作,而无需编写许多长语句。

于 2013-09-09T04:11:23.020 回答