Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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 检查几个字段以执行开关情况_Php_Loops_Switch Statement - Fatal编程技术网

Php 检查几个字段以执行开关情况

Php 检查几个字段以执行开关情况,php,loops,switch-statement,Php,Loops,Switch Statement,我有一个代码要检查几个字段以执行某些代码,如下所示: switch($tag1 || $tag2 || $tag3 || $tag4 ||$tag5){ case "satay": $imgput = "/home/uploads/sandbox/jovine/Food/tags/satay/$img_name"; break; case "digitalmarketing":

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

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;
            }
但它不起作用


任何人都可以提供帮助?

开关
仅支持一个值。仅当条件可以有或和和条件时才有

$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;
                }

以下内容可以帮您解决问题:

<?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语句中使用多个变量,但这将为您提供一个很好的干净的解决方法来执行相同的操作,而无需编写许多长语句。

如果任何
$tag1…5
变量包含
Satay
,您是否希望它输出“Satay”的情况?是的!我的意思是,但它不起作用:switch($tag1 | |$tag2 | |$tag3 | |$tag4 | |$tag5)??有时您只需要在询问之前检查手册->我不确定,但我非常确定switch语句一次只能应用于一个变量。让我看看,我是否可以把一个结构放在一起,将做你需要的。然而,这不是唯一的方法,只是使用一个if-else?我知道你来自马来西亚。沙爹是我最喜欢的,,:)