Reputation: 3
This question requires me to write a PHP code that tells user the temperature based on the region and month they choose in html form. I need to create a file named resultTemp.php as an event handler for my html form. So I have to create a function named getTemp() in the php file which receive the parameter month, and region. This function will check the month, and region. The function is called from resultTemp.php where two parameters(month and region) are passed.
Below is my code in 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
The code runs without an error, but the function echo is not displaying in the browser, any idea what I did wrong thanks in advance.
Upvotes: 0
Views: 60
Reputation: 1056
Here you 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"]);
?>
You were using the &&
instead of the ||
. I also reformatted the code.
Upvotes: 0
Reputation: 146
Your if sections are saying "if the month is both 12, 1 and 2 at the same time" which is never true. Try changing the AND operator (&&) to a OR operator (||)
E.g.:
if ($month == "12" || $month == "1" || $month == "2") {
echo "It is winter. Too cold. Wear thick clothes.";
}
Upvotes: 0
Reputation: 6180
I think you meant to use or (||
) instead of 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
doesn't make any sense.
Upvotes: 1