ELSE声明表现得很有趣

时间:2013-03-02 04:54:43

标签: php

我遇到了一行代码问题。我的老师甚至没有看到问题,我已经打了将近一个半星期。任何帮助将不胜感激。

代码:

{
  if (count($_POST['CINS']) > 0)
  {
    echo "<h2>Your CINS picks are:</h2>\n";
    echo "<ul>\n"; 
    foreach ($_POST['CINS'] as $element)
    {
      echo "\t<li>$element</li>\n";
    } // end of FOREACH statement
    echo "</ul>\n";
  } // end of IF count CINS

  if (count($_POST['CINT']) > 0 ) 
  {
    echo "<h2>Your CINT picks are:</h2>\n";
    echo "<ul>\n"; 
    foreach ($_POST['CINT'] as $element2)
    {
      echo "\t<li>$element2</li>\n";
    } // End of FOREACH CINT
    echo "</ul>\n";
  } // End of IF for CINT
  else
  {
    echo "CINT = " . count($_POST['CINT']) . " CINS = " . count($_POST['CINS']) . "<br />\n";
    echo __LINE__;
    if ((count($_POST['CINT'] == 0)) and (count($_POST['CINS'] == 0)))  // This is where the problem lies. It's showing up the echo statements even when CINS has a count of 1. but if CINT has a count of 1, the echo statements do not show up. 
    {
      echo "<h2>No classes</h2>\n";
      echo "<p>You need to pick a class from BOTH CINT and CINS to be a well rounded student.</p>\n";
      echo "CINT = " . count($_POST['CINT']) . " CINS = " . count($_POST['CINS']) . "<br />\n";
    }
  } // END ELSE COUNT CINS
}
?>

4 个答案:

答案 0 :(得分:3)

错位的括号

if ((count($_POST['CINT'] == 0)) and (count($_POST['CINS'] == 0))) -> wrong

if ((count($_POST['CINT']) == 0) and (count($_POST['CINS']) == 0))

答案 1 :(得分:1)

您不需要所有这些括号:

if(
    count($_POST['CINT']) == 0 AND
    count($_POST['CINS']) == 0
    )

看看一些缩进是如何进行的:

{
    if (count($_POST['CINS']) > 0) {
        echo "<h2>Your CINS picks are:</h2>\n";
        echo "<ul>\n"; 
        foreach ($_POST['CINS'] as $element) {
            echo "\t<li>$element</li>\n";
        }

        echo "</ul>\n";
    }

    if (count($_POST['CINT']) > 0 ) {
        echo "<h2>Your CINT picks are:</h2>\n";
        echo "<ul>\n"; 
        foreach ($_POST['CINT'] as $element2) {
            echo "\t<li>$element2</li>\n";
        }

        echo "</ul>\n";
    }
else {
    echo "CINT = " . count($_POST['CINT']) . " CINS = " . count($_POST['CINS']) . "<br />\n";
    echo __LINE__;

    if(
        count($_POST['CINT']) == 0 AND
        count($_POST['CINS']) == 0
        ) {
        echo "<h2>No classes</h2>\n";
        echo "<p>You need to pick a class from BOTH CINT and CINS to be a well rounded student.</p>\n";
        echo "CINT = " . count($_POST['CINT']) . " CINS = " . count($_POST['CINS']) . "<br />\n";
    }
}

答案 2 :(得分:0)

我想,你想要这个:

if (count($_POST['CINS']) > 0)
{
    …
} // End of IF for CINS

if (count($_POST['CINT']) > 0 ) 
{
    …
} // End of IF for CINT

if (count($_POST['CINS']) == 0 || count($_POST['CINT']) == 0 ) 
{
    …
}

答案 3 :(得分:0)

也许括号需要注意......

if ((count($_POST['CINT'] == 0)) and (count($_POST['CINS'] == 0)))

可能效果更好

if ((count($_POST['CINT']) == 0) && (count($_POST['CINS']) == 0)) 

给它开枪 - 回复结果。