【问题标题】:PHP elseif statement not workingPHP elseif 语句不起作用
【发布时间】:2014-04-25 20:35:17
【问题描述】:

我目前正在开发一个使用 FSQM Pro 测验插件的 wordpress 网站。该插件使用格式字符串 %DESIGNATION% 来显示用户在完成测验时获得的排名。

我正在尝试编写一些 php,它会根据用户达到的排名输出不同的文本,但由于某种原因,它什么也不输出。我的代码如下,有人可以帮忙吗?

这段代码在一个单独的php文件$format_string_components = $this->get_format_string();

<?php
$a="Ranking 1";
$b="Ranking 2";
$c="Ranking 3";
$d="Ranking 4";

if ($format_string_components['%DESIGNATION%'] == $a) {
    echo 'text 1';

} elseif ($format_string_components['%DESIGNATION%'] == $b) {
    echo 'text 2';

} elseif ($format_string_components['%DESIGNATION%'] == $c) {
    echo 'text 3';

} elseif ($format_string_components['%DESIGNATION%'] == $d) {
    echo 'text 4'; 

} else {
    echo '<p>Your result was not found.</p>';
}
?>

【问题讨论】:

  • 请显示这个数组$format_string_components
  • @AdamPool 将“更改为”。
  • 检查您的报价。他们搞砸了。一般来说,如果您不打算在字符串中使用任何字符串魔法,最好使用单引号。
  • 开始 PHP 标记也是 &lt;? php,但它应该是 &lt;?php — 如果这是您在现场网站上的内容,那肯定是个问题。
  • @LucasHenrique 谢谢,它现在正在输出 else 函数。我猜这是因为它不知道$format_string_components['%DESIGNATION%'] 是什么?

标签: php wordpress if-statement


【解决方案1】:

更改为"。去掉空格&lt;? php

<?php
    $a="Ranking 1";
    $b="Ranking 2";
    $c="Ranking 3";
    $d="Ranking 4";

    if ($format_string_components['%DESIGNATION%'] == $a) {
        echo "text 1";

    } elseif ($format_string_components['%DESIGNATION%'] == $b) {
        echo "text 2";

    } elseif ($format_string_components['%DESIGNATION%'] == $c) {
        echo "text 3";

    } elseif ($format_string_components['%DESIGNATION%'] == $d) {
        echo "text 4"; 

    } else {
        echo "<p>Your result was not found.</p>";
    }
?>

【讨论】:

  • 到了一半,谢谢。 $format_string_components 代码位于名为 class-ipt-fsqm-form-elements-data.php 的文件中,显示为 $format_string_components = $this->get_format_string();。尽管如此,它仍然没有返回基于指定的结果。
【解决方案2】:

由于您使用了引号,它无法正常工作。将引号更改为“”。喜欢

if ($format_string_components['%DESIGNATION%'] == $a) {
        echo "text 1";

    } elseif ($format_string_components['%DESIGNATION%'] == $b) {
        echo "text 2";

    } elseif ($format_string_components['%DESIGNATION%'] == $c) {
        echo "text 3";

    } elseif ($format_string_components['%DESIGNATION%'] == $d) {
        echo "text 4"; 

    } else {
        echo "<p>Your result was not found.</p>";
    }

还要避免&lt;?php 标记中的空格。

【讨论】:

    【解决方案3】:
    <?php
    
        $a="Ranking 1";
        $b="Ranking 2";
        $c="Ranking 3";
        $d="Ranking 4";
    
        if ($format_string_components['%DESIGNATION%'] == $a) {
            echo 'text 1';
    
        } elseif ($format_string_components['%DESIGNATION%'] == $b) {
            echo 'text 2';
    
        } elseif ($format_string_components['%DESIGNATION%'] == $c) {
            echo 'text 3';
    
        } elseif ($format_string_components['%DESIGNATION%'] == $d) {
            echo 'text 4'; 
    
        } else {
            echo '<p>Your result was not found.</p>';
        }
    ?>
    

    【讨论】:

      【解决方案4】:

      更改引号 (") 并从

      中删除空格
      <? php
      

      代码使用它:

      <?php
          $a="Ranking 1";
          $b="Ranking 2";
          $c="Ranking 3";
          $d="Ranking 4";
      
          if ($format_string_components['%DESIGNATION%'] == $a) {
              echo "text 1";
          } elseif ($format_string_components['%DESIGNATION%'] == $b) {
              echo "text 2";
          } elseif ($format_string_components['%DESIGNATION%'] == $c) {
              echo "text 3";
          } elseif ($format_string_components['%DESIGNATION%'] == $d) {
              echo "text 4"; 
          } else {
              echo "<p>Your result was not found.</p>";
          }
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-31
        • 2013-01-26
        • 2013-09-13
        • 2020-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-14
        相关资源
        最近更新 更多