【问题标题】:PHP Switch Case not ending statement after "break;"PHP Switch Case 在“break;”之后没有结束语句
【发布时间】:2018-02-26 09:31:16
【问题描述】:

在下面的函数中,如果输入是蓝色的,那么它给出的结果是:“标题内容在这里你最喜欢的颜色是蓝色!”而它应该给出 - “你最喜欢的颜色是蓝色!”,请帮我纠正它。如果输入为红色,则此处的结束语句为:“header content goes here”,这是正确的。

如果我问的是愚蠢的问题,请原谅我,因为我是 PHP 新手,我不知道在 switch 案例中使用函数作为变量是否可以。

test1.php

<?php
function header2() { ?>
   header content goes here
<?php }

$good =header2();
$Input2 = $_POST['input'];

switch ($Input2) {
    case "red":
        echo $good;
        break;
    case "blue":
        echo "Your favorite color is blue!";
        break;
    case "green":
        echo "Your favorite color is green!";
        break;
    default:
        echo "Your favorite color is neither red, blue, nor green!";
}
?>

ma​​in.js

$('[id=stringone]').click(function(){
var string =$('[id=stringotherone]').val();
$.post('test1.php',{input: string}, function(data){
$('[id=stringotherthanone]').text(data);
});
});

test.php

 <! doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="css/hwcb.css">
</head>
<body>
    <input type="button" value="Go" id="stringone">
  <input type="text" id="stringotherone"/>
  <p><div id="stringotherthanone"></div></p>
  <script src="jquery.js"></script>
<script src="js/css.js"></script>
<script src="main.js"></script>
  </body>
  </html>

【问题讨论】:

    标签: php function switch-statement return


    【解决方案1】:

    你需要在函数中return,否则它会在函数被调用时输出。

    示例:https://3v4l.org/1Ptmlhttps://3v4l.org/ug778

    $good =header2(); 输出的是header content goes here,而不是switch 中的echo $good;。这实际上什么也没做,因为那个变量是空的。

    【讨论】:

      【解决方案2】:

      改变

      <?php
      function header2() { ?>
         header content goes here
      <?php }
      

      <?php
      function header2() { 
         return 'header content goes here';
      }
      

      【讨论】:

      • 我认为一个解释会很好:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-17
      • 2011-11-14
      • 1970-01-01
      • 2011-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多