【问题标题】:Check several field to execute switch case检查几个字段以执行 switch case
【发布时间】:2013-09-09 03:53:52
【问题描述】:

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

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

但它不起作用。

有人可以帮忙吗?

【问题讨论】:

  • 如果任何$tag1...5 变量包含satay,您是否希望它输出“Satay”的大小写?
  • 是的!我的意思是,但它不起作用这有什么问题吗:switch($tag1 || $tag2 || $tag3 || $tag4 ||$tag5) ??跨度>
  • 有时您只需要在询问之前查看手册 -> php.net/manual/en/control-structures.switch.php
  • 我不确定,但我很确定 switch 语句一次只能应用于一个变量。让我看看我是否可以组合一个结构来满足您的需求。
  • 这不是使用 if else 的唯一方法吗?

标签: php loops switch-statement


【解决方案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;
                }

【讨论】:

  • 我认识你来自马来西亚。沙爹是我的最爱,,:)
【解决方案2】:

以下内容应该可以为您解决问题:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-16
    • 2012-02-25
    • 2021-08-05
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 2014-11-09
    相关资源
    最近更新 更多