如果Post值等于其他post值以外的回显“失败”;?

时间:2012-09-03 12:07:00

标签: php sql

我有两个组合框,一个用于报告分数,一个用于设置进球的目标,如果在帖子['submit']

,我怎么能这样做?

如果$ _POST ['Score1']和$ _POST ['Score2'] 不等于 $ _POST ['homegoalscorer1']和$ _POST ['awaygoalscorer1'] 然后回声“失败”;

喜欢的东西;

 if(isset($_POST['submit']))   
 {
 $homescore = $_POST['Score1'];
 $awayscore = $_POST['Score2'];

 $homegoalscorer = $_POST['homegoalscorer1'];
 $awaygoalscorer =  $_POST['awaygoalscorer1'];

   if '$homescore' + '$awayscore' != $homegoalscorer + $awaygoalscorer {
      echo "failed";
   }
      else {

            }
  }

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

变量上的单引号会将该变量转换为字符串而不执行。你也忘了添加括号:

if(isset($_POST['submit']))   {
    $homescore = (float)$_POST['Score1'];
    $awayscore = (float)$_POST['Score2'];

    $homegoalscorer = (float)$_POST['homegoalscorer1'];
    $awaygoalscorer =  (float)$_POST['awaygoalscorer1'];

    if (($homescore+$awayscore) != ($homegoalscorer+$awaygoalscorer)) {
        echo "failed";
    } else {

    }
}

答案 1 :(得分:1)

在if语句中使用一些括号来强制条件设置在正确的上下文中 - 为什么要将变量封装在单引号中?

if (($homescore + $awayscore) != ($homegoalscorer + $awaygoalscorer))
{
// Your code continues....