PHP if或statement无法按预期工作

时间:2015-08-18 17:49:40

标签: php if-statement logical-operators

我试图遍历我的$ _POST变量并显示只有key = lap *的数据。这是我到目前为止的代码。

foreach($_POST as $key=>$val)
{
    if($key != "time" || $key != "avgspd" || $key != "kartno")
    {
        echo $key . "<br>";
    }
}

它给我的结果就是这个

time
lap1
lap2
lap3
lap4
lap5
lap6
lap7
lap8
lap9
lap10
lap11
lap12
lap13
lap14
lap15
lap16
lap17
avgspd
kartno

如您所见,它仍会显示关键avgspdkartnotime。奇怪而令人困惑的是,如果我将代码更改为:

foreach($_POST as $key=>$val)
{
    if($key == "time" || $key == "avgspd" || $key == "kartno")
    {
        echo $key . "<br>";
    }
}

我得到了这个结果:

time
avgspd
kartno

这没有任何意义。如果我检查密钥是否不等于timeavgspdkartno它会显示所有密钥,但当我说它只显示密钥“time”,“avgspd”或“kartno”它按预期工作!这里发生了什么?我需要做些什么来解决它。是否有一些早期代码导致错误?如果是这样,这是我的代码。

的index.php

<?php
    if(!$_POST)
    {
        include("functions.php");
        echo "<link rel='stylesheet' type='text/css' href='style.css'>";
        echo "<form action='' method='POST'>";
        echo "<table><tr>";
        table_head("Time","lap 1","lap 2","lap 3","lap 4","lap 5","lap 6","lap 7","lap 8","lap 9","lap 10","lap 11","lap 12","lap 13","lap 14","lap 15","lap 16","lap 17","Avg Spd");
        echo "</tr><tr>";
        display_fields();
        echo "</tr></table>";
        echo "Kart Number: <input type='text' size='2' name='kartno'/><br>";
        echo "<input type='submit'/>";
        echo "</form>";
    } else {
        foreach($_POST as $key=>$val)
        {
            if($key == "time" || $key == "avgspd" || $key == "kartno")
            {
                echo $key . "<br>";
            }
        }
    }
?>

的functions.php

<?php
    function table_head()
    {
        if ( func_num_args() > 0 )
        {
            $args = func_get_args();
            foreach($args as $value)
            {
                echo "<th>" . $value . "</th>";
            }
        }
    }


    function display_fields()
    {
        $i = 0;
        $a = 19;
        $name="hi";
        while($i++ < $a)
        {
            array_push($numbers,$i);
            if($i==1)
            {
                $name = "time";
            } else if($i > 1 && $i < 19){
                $name = "lap" . strval($i-1);
            } else {
                $name = "avgspd";
            }
            echo "<td><input type='text' size='8' name='" . $name . "'/></td>";
        }
    }
?>

3 个答案:

答案 0 :(得分:2)

这始终是真的:

if($key != "time" || $key != "avgspd" || $key != "kartno")

你的意思是:

if($key != "time" and $key != "avgspd" and $key != "kartno")

您可以将其重写为:

if(! in_array($key,array("time","avgspd","kartno")))

答案 1 :(得分:2)

我相信你正在寻找&&。或者我错过了什么。

foreach($_POST as $key=>$val)
{
    if($key != "time" && $key != "avgspd" && $key != "kartno")
    {
        echo $key . "<br>";
    }
}

您希望满足所有三个条件才能打印。

编辑:更好!检查它是否以lap开头。这更适合你想要的东西。

foreach($_POST as $key=>$val)
{
    if(substr($key, 0, 3) == "lap")
    {
        echo $key . "<br>";
    }
}

答案 2 :(得分:1)

你走了:

foreach($_POST as $key=>$val)
{
    if($key != "time" && $key != "avgspd" && $key != "kartno")
    {
        echo $key . "<br>";
    }
}

解释:如果你在这里放置OR语句,它将始终打印,因为你基本上告诉你的程序打印它,如果它不同于“时间”或不同于“avgspd”。并且“时间”与“avgspd”不同,所以它打印出来。