简体   繁体   中英

else if is not working in my php code , what should i do?

Hello guys i have a code that receive values from the another page and i want to check if the value equal with on of one values in the array then give me that value so this is my code :

**<?php
   session_start();
ob_start();
 //where we get data from the form
if (isset($_POST["btn"])){
    $name=$_POST["name"];
$number=$_POST["number"];
$date=$_POST["fd"].'/'. $_POST["sd"].'/' .$_POST["td"];
    echo $name;
    echo"</br>";
    echo $number;
    echo"</br>";
    echo $date;

    }
$ar=sscanf($date,"%d/%d/%d");

$month1=$ar[1]; // for example the value is 5

from the here i have array

$month=array(
    "juan"  =>"1",
    "feb"   =>"2",
    "march" =>"3",
    "april" =>"4",
    "may"   =>"5",
    "jun"   =>"6",
    "july"  =>"7",
    "agu"   =>"8",
    "sep"   =>"9",
    "oct"   =>"10",
    "nov"   =>"11",
    "dec"   =>"12"

);
function monthdate($m,$m1){

$flag=false;
foreach ($m1 as $key=>$val){

    $val1=$val;

    if ($val1 == $m){
    $flag=true;
      break;
    }

    else if(!$val1 == $m) {
    $flag=false;}

}
if($flag == true ){
    echo $val1;}

}
echo "your birthday month is ". monthdate($month1,$month);
echo"</br>";
?>**

why the function doesn't give me any data ?

This is a very strange condition, and probably isn't what you think it is:

if(!$val1 == $m)

You're negating $val1 (which may not do what you think it does for non-boolean values) and comparing the result of that with $m . Surely you meant this?:

if($val1 != $m)

Though, even still, you don't need this condition at all. Your original if is comparing these values:

if ($val1 == $m)

Following that, all cases where they do not equal will move to the else block. There's no need for an explicit if on that else block if it just repeats the negative of the original if condition.

Even further, your else condition likely isn't necessary in this function at all. Consider the logic you're implementing... A value is false until the first matching result is found, then it's true and the loop (and function) completes and exits. There's no reason to ever reset the value to false , so the entire else block is unnecessary.


Aside from that, based on your usage of the function it looks like you had meant to return $val1 instead of echo $val1 . In the code you're using the two approaches are likely to produce identical results so it may make no difference here, but it's important to understand the difference between the two or you're sure to encounter strange bugs in the future.


Edit: Based on your comment below, it sounds like you also want some kind of default value for input which doesn't match the data. You still don't necessarily need the else , you can rely on control flow at the end of your function. For example, where you currently have this:

if ($flag == true){
    echo $val1;
}

You could do this:

if ($flag == true){
    echo $val1;
} else {
    echo "Not found!";
}

Or, if you switch to return instead of echo as suggested above, you could do the same with slightly less code:

if ($flag == true){
    return $val1;
}
return "Not found!";

Or even:

return $flag == true ? $val1 : "Not found!";

You can directly parse the loop and when loop gets the required month and monthname, you can directly break the loop or you can return from the function

 <?php
    $month1=5;
    $month=array(
        "juan"  =>"1",
        "feb"   =>"2",
        "march" =>"3",
        "april" =>"4",
        "may"   =>"5",
        "jun"   =>"6",
        "july"  =>"7",
        "agu"   =>"8",
        "sep"   =>"9",
        "oct"   =>"10",
        "nov"   =>"11",
        "dec"   =>"12"

    );

    function monthdate($m, $m1)
    {
        foreach ($m1 as $key => $val) 
        {
            if ($val == $m) 
            {
                return $key;
            }
        }    
    }
    echo "Your birthday month is " . monthdate($month1, $month);
    ?>

But in my opinion, using ID is a better option with respect to words for comparison.You can achieve the same above result like:

<?php
$month1=5;
$month = array(
    "1"=>"jan",
    "2"=>"feb",
    "3"=>"mar",
    "4"=>"apr",
    "5"=>"may",
    "6"=>"jun",
    "7"=>"jul",
    "8"=>"aug",
    "9"=>"sep",
    "10"=>"oct",
    "11"=>"nov",
    "12"=>"dec"
);

function monthdate($m, $m1)
{
    foreach ($m1 as $key => $val) 
    {
        if ($key == $m) 
        {
            return $val;
        }
    }    
}
echo "Your birthday month is " . monthdate($month1, $month);
?>

If you have the option to select value greater than 12,

<?php
    $month1=5;
    $month=array(
        "juan"  =>"1",
        "feb"   =>"2",
        "march" =>"3",
        "april" =>"4",
        "may"   =>"5",
        "jun"   =>"6",
        "july"  =>"7",
        "agu"   =>"8",
        "sep"   =>"9",
        "oct"   =>"10",
        "nov"   =>"11",
        "dec"   =>"12"

    );

    function monthdate($m, $m1)
    {
        $flag='';
        foreach ($m1 as $key => $val) 
        {
            if ($val == $m) 
            {
                $flag=$key;
                break;
            }
        }

        return $flag;
    }

    $d = monthdate($month1, $month);
    if(trim($d)==null)
    {
        echo 'Not found';
    }
    else
    {
         echo "Your birthday month is " .$d ;
    }

?>
$month=array(
    "juan"  =>"1",
    "feb"   =>"2",
    "march" =>"3",
    "april" =>"4",
    "may"   =>"5",
    "jun"   =>"6",
    "july"  =>"7",
    "agu"   =>"8",
    "sep"   =>"9",
    "oct"   =>"10",
    "nov"   =>"11",
    "dec"   =>"12"

);

function Search($value, $array) 
{ 
    return(array_search($value, $array)); 
}

$entered_month= '13';
if($entered_month > 0 && $entered_month <=12){
    echo "your birthday month is ". Search($entered_month,$month);
    echo"</br>";
}else{
  echo "Please enter a valid months";
  echo"</br>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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