变量undefined [重复]

I'm setting Error Variable to contain all the errors if user left any of the input and them I'm trying to echo it out in a div but I get this message:

"Notice: Undefined variable: error in C:\xampp\htdocs\signup.php on line 202"

Although I've defined this Variable earlier in first php tags but I still get Error..

<?php 

session_start();

if(isset($_POST['signup'])){

    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $email = $_POST['email'];
    $pass = $_POST['pass'];
    $cpass = $_POST['cpass'];

    $error="";

        if(!$fname){

        $error.="<br />Please enter your First Name";

        }

        if(!$lname){

           $error.="<br />Please enter your Last Name";

        }

        if(!$email){

            $error.="<br />Please enter your Email Adress";

        }

        if(!$pass && strlen($_POST['pass'])<8){

            $error.="<br />Please enter your Password with atleast 8 Characters.";

        }

        if(!$cpass){

            $error.="<br />Please enter your Confirmed Password";

        }

        if($pass != $cpass){

            $error.="<br />Your Passwords don't match";

        }

        if(!$error){

            $link = mysqli_connect("localhost", "root", "", "shop");

            $query = "SELECT * FROM users WHERE email='".mysqli_real_escape_string($link, $email)."'";

            $result = mysqli_query($link, $query);

            $results = mysqli_num_rows($result);

            if($results == 1){

                $error = "This Email is already Registered. Do you want to <a href='login.php'>Login </a>?";

            }else{

                $query = "INSERT INTO users (fname, lname, email, password) VALUES ('".$fname."', '".$lname."', '".mysqli_real_escape_string($link, $email)."', '".md5(md5($email).$pass)."')";

                $result = mysqli_query($link, $query);

            if($result == FALSE){

                $error = "Couldn't Connect";

            }else{

                $_SESSION['id']=mysqli_insert_id($link);

                print_r($_SESSION);

                /* Redirect to Logged In Page */

            }
        }

        }else{

            $error = "There were following error(s) in your Sign Up Details.".$error;

            echo '<div class="alert alert-danger">'.$error.'</div>';

        }

}

?>

'.$error.'';

            }

       ?&gt;
     <form method="post" action="">

       <div class="form-group inputs">

          <span>First Name: </span><input type="text" name="fname" class="form-control" style="margin-right:20px" />

          <span>Last Name: </span><input type="text" name="lname" class="form-control" />

       </div> <!-- end form-group Div -->

       <div class="form-group inputs">

          <span>Email: </span><input type="text" style="width:350px" name="email" class="form-control" />

       </div> <!-- end form-group Div -->

       <div class="form-group inputs">

          <span style="margin-left:-33px;">Password: </span><input type="password" style="width:350px;" name="pass" class="form-control" />

       </div> <!-- end form-group Div -->

       <div class="form-group inputs">

          <span style="margin-left:-102px;">Confirm Password: </span><input type="password" style="width:350px" name="cpass" class="form-control" />

       </div> <!-- end form-group Div -->

       <div style="margin:10px auto 10px auto; width:70%; text-align:center">

          <span>By registering your Account in our Online Store, You agree to our <a href="#">Terms And Conditions</a> and Confirm that you have read our <a href="#">User Guide</a>.</span>

       </div>

       <div class="form-group center">

          <input type="submit" value="Sign Up" name="signup" class="btn btn-success btn-lg">

     </form>
</div>

The problem is that $error is only defined within the if (isset($_POST['signup'])) { condition.

You'll need to define it outside, or just check if it's set when you try to use it, e.g.

if (isset($error)) {

If you want to check that it's also not blank, you can do

if (isset($error) && $error !== '') {

although

if (isset($error) && $error) {

will also work, as a non-blank, non-zero value will equate to true. But I think the first option is more readable.