否则,如果在我的PHP代码中不起作用,我该怎么办?

时间:2019-12-10 11:38:08

标签: php if-statement foreach

大家好,我有一个代码可以从另一页接收值,我想检查该值是否等于数组中一个值中的一个,然后给我该值,所以这是我的代码:

**<?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

从这里我有数组

$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>";
?>**

为什么函数没有给我任何数据?

5 个答案:

答案 0 :(得分:1)

  

您可以直接解析循环,并在循环获得所需月份时进行解析   和monthname,您可以直接中断循环,也可以从   功能

 <?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);
    ?>

但是我认为,对于单词进行比较,使用ID是更好的选择。您可以实现与上述相同的结果:

<?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);
?>
  

如果您可以选择大于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 ;
    }

?>

答案 1 :(得分:0)

这是一个非常奇怪的情况,可能不是您认为的那样:

if(!$val1 == $m)

您正在求反 $val1(对于非布尔值,它可能不执行您认为的工作),并将结果与​​$m进行比较。您肯定是这个意思吗?:

if($val1 != $m)

尽管,即使如此,您根本不需要此条件。您原来的if正在比较这些值:

if ($val1 == $m)

在此之后,所有不相等的情况将移至else块。如果只是重复原始if条件的负数,则无需在该else块上使用明确的if

更进一步,此功能完全不需要您的else条件。考虑一下您要实现的逻辑...值是false,直到找到第一个匹配结果,然后是true,循环(和函数)完成并退出。没有理由将值重置为false,因此不需要整个else块。


除此之外,根据您对函数的使用情况,您似乎打算return $val1而不是echo $val1。在您使用这两种方法的代码中,可能会产生相同的结果,因此在这里可能没有什么区别,但是了解两者之间的区别很重要,否则您将来一定会遇到奇怪的错误。


编辑:根据下面的评论,听起来您还想要某种与数据不匹配的默认输入值。您仍然不一定需要else,您可以在函数末尾依靠控制流。例如,您当前所在的位置:

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

您可以这样做:

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

或者,如果您切换到return而不是上面建议的echo,则可以用更少的代码来做相同的事情:

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

甚至:

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

答案 2 :(得分:0)

$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>";
}

答案 3 :(得分:-1)

您必须在函数末尾使用return而不回显

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<table class="messages-list-table">
  <tr>
    <th class="message-name">Name</th>
    <th class="message-body">Message</th>
  </tr>  
  <tr id="1">
    <td class="message-name">Mike</td>
    <td class="message-body" data-code="5652c0bcc277bf4c039686d">This is the same message</td>
  </tr>
  <tr id="2">
    <td class="message-name">John</td>
    <td class="message-body" data-code="5652c0bcc277bf4c039686d">This is the same message</td>
  </tr>
  <tr id="3">
    <td class="message-name">Steve</td>
    <td class="message-body" data-code="725c1424b123dabc3">This is the same messages</td>
  </tr>
  <tr id="4">
    <td class="message-name">Michael</td>
    <td class="message-body" data-code="5652c0bcc277bf4c039686d">This is the same message</td>
  </tr>
  <tr id="5">
    <td class="message-name">Alfredo</td>
    <td class="message-body" data-code="725c1424b123dabc3">This is the same messages</td>
  </tr>
  <tr id="6">
    <td class="message-name">Paul</td>
    <td class="message-body" data-code="8cbsh7364hd94d8">Totally different message</td>
  </tr>
  <tr id="7">
    <td class="message-name">Kuldip</td>
    <td class="message-body" data-code="dsdasdw4394839482384">Demo message but different from all</td>
  </tr>
</table>

答案 4 :(得分:-1)

这是您的答案。用

替换monthdate函数

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 ){
        return $val1;
    }

}