不能在代码的其他位置使用IF语句中定义的变量


cannot use a variable defined in an IF statement in other places in the code

我对以下代码有问题。我看到这个话题在这个论坛上被大量提及,但我仍然找不到解决方案。我不明白的是,为什么在所有的场景中,代码都是正确的,注释$comment1和$comment2可以从下一个将表单发送回用户的if语句中访问,但只有在一个if语句中(由代码中的注释声明),这种情况没有正确发生。感谢您的时间和努力。

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$veremail = $_POST['veremail'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// All these variables receive values from a form
// The following IF() statements are supposed to answer all the possible
// outcomes of an email verification script, which checks if the email
// address was entered in two different fields in the same way
 if ($email===$veremail and !empty($email) and !empty($veremail)){
    $comment1='';
    $comment2=''; 
}
if ($email===$veremail and empty($email) and empty($veremail)){
    $comment1='*Please type';
    $comment2='*Please type'; 
 }
//The next if statement is the one i am having trouble with, whenever the code
//in this statement is executed, $comment 1 and $comment 2 are not displayed
//in the if statement that resend the user to the form when a problem of typing
//the data was encountered** 
     if($email!=$veremail and !empty($email) and !empty($veremail)){
        $comment1='*email addresses not match';
        $comment2='*email addresses not match'; 
     }**
 if ($email!=$veremail and !empty($email) and empty($veremail)){
    $comment1='';
    $comment2='*Please type'; 
 }
 if($email!=$veremail and empty($email) and !empty($veremail)){
    $comment1='*Please type';
    $comment2='';
 }
echo    $comment1.' ' .$comment2;  //I added this to make sure these variables
// received the expected value by the preceding if statements, I never
// encounterd a problem in here: the expected values were always presented
// correctly here
 /* 
The next code checks variables submitted from form customer feedback
 This next code analyses the variables submitted by the form using the
 empty() function If any of them are empty it will return the client to
 the form, the values which were already typed are kept and a note next
 to a field which was not submitted is added
 */
if (    empty($fname) 
    OR  empty($fname) 
    OR  empty($lname) 
    OR  empty($email) 
    OR  empty($veremail) 
    OR  empty($subject) 
    OR  empty($message) 
    OR  $email!=$veremail){
echo "
<center>    'n
<br><br><br>'n
<h1>Your feed back please</h1>'n
<Br><br>'n
    't<form action=".'feedbackprocess.php'." method=".'POST'.">'n
    't't<table>'n
    't't't      <tr>'n
    't't't't        <td>First name:             </td>           <td><input type=".'text'." name=".'fname'." value="."$fname".">";
                    if(empty($fname)){
                    echo "*please fill";
                    }
                    echo"</td>'n
    't't't      </tr>'n
    't't't      <tr>'n
    't't't't        <td>Last name:              </td>           <td><input type=".'text'." name=".'lname'." value="."$lname".">";
                    if(empty($lname)){
                    echo "*please fill";
                    }
                    echo"</td>'n
    't't't      </tr>'n
    't't't      <tr>'n
    't't't't        <td>Email address:          </td>           <td><input type=".'text'." name=".'email'." value="."$email".">";
                    if(empty($email)){
                    echo "$comment1";
                    }
                    echo"</td>'n
                </tr>'n
    't't't      <tr>'n
    't't't't        <td>Verify email address:   </td>           <td><input type=".'text'." name=".'veremail'." value="."$veremail".">";
                    if(empty($veremail)){
                    echo "$comment2";
                    }
                    echo"</td>'n
    't't't      <tr>'n
                <tr>'n
    't't't't        <td>Subject:                </td>           <td><input type=".'text'." name=".'subject'." value="."$subject".">";
                    if(empty($subject)){
                    echo "*please fill";
                    }
                    echo"</td>'n
    't't't      </tr>'n
    't't</table>'n
        <br><br>'n
                            Content:<br>'n
                    <textarea rows=".'6'." cols=".'50'." name=".'message'.">$message"."</textarea>";
                    if(empty($message)){
                    echo "<br><br>*please fill";
                    }
                    echo"
                    <br><br>'n
                    <input type=".'submit'.">'n
                    </form>'n
</center>'n
";
}else{
echo 'Thank you for your comment';
}

一次检查一件事,不需要时不要与AND连锁。

像这样的东西可能不是你想要的:

if (empty($email) and empty($veremail)) {
     // Handle error
}

只有当$email$veremail都为空时,才会触发此项!因此,如果只有一个字段为空,则程序不会注册错误。

让我们来看一下给您带来麻烦的if子句,即以下内容:

 if($email!=$veremail and !empty($email) and !empty($veremail)){
    $comment1='*email addresses not match';
    $comment2='*email addresses not match'; 
 }

这里的问题是,您只想检查$email != $veremail。事先检查变量是否为空,这样我们就不必再担心变量为空了。一步一个脚印。:)

像这样的东西会起作用的。请注意or而不是and。首先我们检查$email$veremail不为空,然后我们检查它们是否相等。

if (empty($email) or empty($veremail)) {
    $comment = '*Please type';
}
else if ($email !== $veremail) {
    $comment = '*Email addresses did not match';
}

在响应字段中:

if(empty($email)){ echo "$comment1"; }

if(empty($veremail)){ echo "$comment2"; }

它将隐藏注释,因为这两个字段都不是空的,因此它隐藏注释。尝试使用

if($comment){ echo "$comment1"; }

只有$comment变量中有任何内容时,才会显示此代码。

注意。。最好使用:

if($variable}{}

要检查变量中是否有任何数据。如果已填充,则返回false,请使用:

if(!$variable}{}

希望这能有所帮助。

感谢大家的解决方案,我从中学到了很多。问题是合乎逻辑的:

本应输入值电子邮件地址与变量$comment1不匹配的第一个条件是:

if($email!=$veremail and !empty($email) and !empty($veremail)){
        $comment1='*email addresses not match';
        $comment2='*email addresses not match'; 

然而,当我想在用户被引用回表单时显示这些评论时,这是代码:

<tr>
    <td>Email address:</td>           
    <td><input type=".'text'." name=".'email'." value="."$email".">";
                        if(empty($email)){
                        echo "$comment1";
                        }
    echo"</td>
</tr>
<tr>
     <td>Verify email address:</td>           
     <td><input type=".'text'." name=".'veremail'." value="."$veremail".">";
                        **if(empty($veremail)){
                        echo "$comment2";
                        }**
      echo"</td>
<tr>

此条件不允许显示注释$comment1和$comment2,删除此条件后,问题就解决了。