繁体   English   中英

PHP:将后值传递到 function

[英]PHP: passing post value into function

问题

这个问题需要我编写一个 PHP 代码,告诉用户基于他们在 html 表格中选择的区域和月份的温度。 我需要创建一个名为 resultTemp.php 的文件作为我的 html 表单的事件处理程序。 所以我必须在 php 文件中创建一个名为 getTemp() 的 function,它接收参数月份和区域。 这个 function 将检查月份和地区。 function 从 resultTemp.php 调用,其中传递了两个参数(月份和地区)。

下面是我在 resultTemp.php 中的代码

//check post value
if (strlen($_POST["month"]) > 0) {
  $month = $_POST["month"];
} else {
  echo "select the month you desire!";
}

if (strlen($_POST["region"]) > 0) {
  $region = $_POST["region"];
} else {
  echo "select the region you wanna visit!";
}

echo "Month: " . $month . "<br>";
echo "Month: ". $region . "<br>";




//function declaration

function getTemp($region, $month) {
  if ($region == "North") {
    if ($month == "12" && $month == "1" && $month == "2") {
      echo "It is winter. Too cold. Wear thick clothes.";
    }
    elseif($month == "3" && $month == "4" && $month == "5") {
      echo "It is spring. Nice temperature. Jacket is enough.";
    }
    elseif($month == "6" && $month == "7" && $month == "8") {
      echo "It is summer. Hot.";
    }
    elseif($month == "9" && $month == "10" && $month == "11") {
      echo "It is autumn. Nice temperature. Jacket is enough.";
    }
  }

  elseif($region == "South") {
    if ($month == "6" && $month == "7" && $month == "8") {
      echo "It is winter. Too cold. Wear thick clothes.";
    }
    elseif($month == "9" && $month == "10" && $month == "11") {
      echo "It is spring. Nice temperature. Jacket is enough.";
    }
    elseif($month == "12" && $month == "1" && $month == "2") {
      echo "It is summer. Hot.";
    }
    elseif($month == "3" && $month == "4" && $month == "5") {
      echo "It is autumn. Nice temperature. Jacket is enough.";
    }
  } else {
    echo "<br>".
    "undefined parameter";
  }
}

echo "<br>";
getTemp($_POST["region"], $_POST["month"]);//call function 

代码运行没有错误,但 function 回显未显示在浏览器中,知道我做错了什么提前谢谢。

我认为您的意思是使用or ( || ) 而不是and ( && ):

function getTemp($region, $month) {
  if ($region == "North") {
    if ($month == "12" || $month == "1" || $month == "2") {
      echo "It is winter. Too cold. Wear thick clothes.";
    } elseif($month == "3" || $month == "4" || $month == "5") {
      echo "It is spring. Nice temperature. Jacket is enough.";
    } elseif($month == "6" || $month == "7" || $month == "8") {
      echo "It is summer. Hot.";
    } elseif($month == "9" || $month == "10" || $month == "11") {
      echo "It is autumn. Nice temperature. Jacket is enough.";
    }
  } elseif($region == "South") {
    if ($month == "6" || $month == "7" || $month == "8") {
      echo "It is winter. Too cold. Wear thick clothes.";
    } elseif($month == "9" || $month == "10" || $month == "11") {
      echo "It is spring. Nice temperature. Jacket is enough.";
    } elseif($month == "12" || $month == "1" || $month == "2") {
      echo "It is summer. Hot.";
    } elseif($month == "3" || $month == "4" || $month == "5") {
      echo "It is autumn. Nice temperature. Jacket is enough.";
    }
  } else {
    echo "<br>".
    "undefined parameter";
  }
}

$month == 6 && $month == 7 && $month == 8没有任何意义。

您的 if 部分说“如果月份同时是 12、1 和 2”,这绝不是真的。 尝试将 AND 运算符 (&&) 更改为 OR 运算符 (||)

例如:

if ($month == "12" || $month == "1" || $month == "2") {
  echo "It is winter. Too cold. Wear thick clothes.";
}

这里是 go:

<?php
if(strlen($_POST["month"]) > 0){
  $month = $_POST["month"];
}
else{
  echo "select the month you desire!";
}

if(strlen($_POST["region"]) > 0){
  $region = $_POST["region"];
}
else{
  echo "select the region you wanna visit!";
}

echo "Month: ".$month."<br>";
echo "Month: ".$region."<br>";

//call function

function getTemp($region, $month){
  if($region == "North"){
    if($month == "12" || $month == "1" || $month == "2"){
      echo "It is winter. Too cold. Wear thick clothes.";
    }
    elseif($month == "3" || $month == "4" || $month == "5"){
      echo "It is spring. Nice temperature. Jacket is enough.";
    }
    elseif($month == "6" || $month == "7" || $month == "8"){
      echo "It is summer. Hot.";
    }
    elseif($month == "9" || $month == "10" || $month == "11"){
      echo "It is autumn. Nice temperature. Jacket is enough.";
    }
  }
    
  elseif($region == "South"){
    if($month == "6" || $month == "7" || $month == "8"){
      echo "It is winter. Too cold. Wear thick clothes.";
    }
    elseif($month == "9" || $month == "10" || $month == "11"){
      echo "It is spring. Nice temperature. Jacket is enough.";
    }
    elseif($month == "12" || $month == "1" || $month == "2"){
      echo "It is summer. Hot.";
    }
    elseif($month == "3" || $month == "4" || $month == "5"){
      echo "It is autumn. Nice temperature. Jacket is enough.";
    }
  }
  else{
    echo "<br>"."undefined parameter";
  }
}
echo "<br>";
getTemp($_POST["region"], $_POST["month"]);
?>

您使用的是&&而不是|| . 我还重新格式化了代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM