我的PHP代码中缺少大括号

时间:2012-03-10 16:24:41

标签: php

我错过了一个我找不到的花括号。 解析错误显示在我的代码末尾。我已经花了很长时间。我不太了解如何识别这些错误的PHP提示会有所帮助。

<?php 

                /*Required Fields*/
                require_once('websiteconfig.inc.php');
                include (ABSOLUTE_PATH . 'class/person.class.php');

                /*Starts the session for the person class*/
                session_start();


                /*FUNCTIONS*/

                /*VERRIFY EMAIL ADDRESS AND PASSWORD AND MATCH IN SYSTEM*/
                function validateLogin($emailaddress='', $password=''){

                /*INITIALIZES VARIABLES*/
                $email_key = 'betty@abc.com';
                $password_key = '1234';

                $auth_match = 0;

                /* CHECK FOR MATCH */
                if($emailaddress == $email_key && $password == $password_key){
                    $auth_match = 1;
                    }
                return $auth_match;
                }

                /*CLEAN FORM DATA*/
                function sanitize($form_var) {
                    $clean_data = strtolower(trim($form_var));

                    return $clean_data;
                }

                /*PAGE VARIABLES*/
                $auth_status = 0;

                /*DETERMINE FORM HAS BEEN SUBMITTED*/
                if(array_key_exists('submit', $_POST)) {

                /*SANITIZE FORM DATA*/
                $emailaddress = sanitize($_POST['emailaddress']);
                $password = sanitize($_POST['password']);

                /*VALIDATE FORM DATA*/
                $auth_status = validateLogin($emailaddress, $password); 

                }


                /*CONFIRM EACH FIELD WAS PROCESSED*/
                //trigger login field exception
                try{
                    if($emailaddress == '' || $password == ''){
                        throw new Exception('E-mail and password must be supplied to login.  Please try again.');
                    }else{
                        /*VALIDATE FOR DATA*/
                        $auth_status = validateLogin($emailaddress, $password);

                        //trigger validation exception


                        try{
                            if(!isset($auth_status)) {
                                throw new Exception('Online Banking is not available at this time.  Please try again later.');
                            }
                        }



                        // catch validation exception
                        catch(Exception $v) {
                            echo 'message: ' . $v->getMessage();
                            exit();
                        } //end catch

                        //This triggers the validation of authentication
                        try{
                            if ($auth_status == 0){
                            throw new Exception('Email address and or Password does not meet our records!  Please try again.');
                            }elseif ($auth_status>0){
                                /*Member Session*/
                                $currentMember = new Person($auth_status);

                                /*Set Attributes*/
                                $currentMember->memberid = $auth_status;
                                $currentMember->firstname = 'Michael';
                                $currentMember->lastname = 'Crawley';
                                $currentMember->emailaddress = 'MichaelCrawley@sort.com';

                                /*Serialize currentMember object*/
                                $_SESSION['currentMember'] = serialize($currentMember);

                    }
                }// End try statment

                //catch login field exception
                catch(Exception $e) {
                    echo 'Message: ' . $e->getMessage();
                    exit();
                        }                   

            ?>





        </div><div class="container" id="shadow">
        <div>
            <?php 

                include(ABSOLUTE_PATH . 'header.inc.php');

                if($auth_status == 1){
                    /*AUTHENTICATION SUCCESS*/
                    echo '<h4>Welcome Back, Betty!</4>' . "\n\n";
                    echo '<ul>' . "\n";
                    echo "\t"  . '<li><a href="' . APP_ROOT . 'onlinebanking" title="Online Banking">Online Banking</a></li>' . "\n\n";
                    echo '</ul>';


                } elseif($auth_status == 0){
                    /*AUTHENTICATION FAILED*/
                    echo '<h4 class="error">Authentication Error!</h4>' . "\n\n";
                    echo '<p>Incorrect e-mail address and/or password submitted. Please try again.</p>';
                }

            ?>




                </div><!--End of main content-->
            <?php 
                include(ABSOLUTE_PATH . 'footer.inc.php');
            ?>

</div>

5 个答案:

答案 0 :(得分:2)

确保正确的括号匹配的一种常用方法是将每个{}的内容缩进一个标签而不是外部。

答案 1 :(得分:0)

您错过了源代码中第54行打开的“尝试”构造的“捕获”。

答案 2 :(得分:0)

将其添加到代码顶部

error_reporting(E_ALL);

但是当您找不到错误时,请开始删除代码段并再次运行文件网页,直到错误消失。

例如从代码中删除include footer.inc.php,删除一些PHP代码以实际查找导致错误的原因。然后,您可以慢慢地添加代码以查看触发错误的内容。

答案 3 :(得分:0)

通常,这不是SO的用途,但是......

if($emailaddress == '' || $password == ''){
  throw new Exception('E-mail and password must be supplied to login.  Please try again.');
}else{
  /*VALIDATE FOR DATA*/

这里的“其他”似乎没有匹配的右括号。至少,根据我的文本编辑器,它有支撑匹配。 (值得研究......)

更新:实际上,您似乎缺少2个关闭括号。请参阅下面的重新格式化代码: (请参阅### THIS BRACE IS UNMATCHED ###标记。)

<?php 
/*Required Fields*/
require_once( 'websiteconfig.inc.php' );
include( ABSOLUTE_PATH . 'class/person.class.php' );

/*Starts the session for the person class*/
session_start();

/*FUNCTIONS*/

/*VERRIFY EMAIL ADDRESS AND PASSWORD AND MATCH IN SYSTEM*/
function validateLogin($emailaddress='', $password=''){

  /*INITIALIZES VARIABLES*/
  $email_key = 'betty@abc.com';
  $password_key = '1234';

  $auth_match = 0;

  /* CHECK FOR MATCH */
  if($emailaddress == $email_key && $password == $password_key){
    $auth_match = 1;
  }
  return $auth_match;
}

/*CLEAN FORM DATA*/
function sanitize($form_var) {
  $clean_data = strtolower(trim($form_var));

  return $clean_data;
}

/*PAGE VARIABLES*/
$auth_status = 0;

/*DETERMINE FORM HAS BEEN SUBMITTED*/
if(array_key_exists('submit', $_POST)) {

  /*SANITIZE FORM DATA*/
  $emailaddress = sanitize($_POST['emailaddress']);
  $password = sanitize($_POST['password']);

  /*VALIDATE FORM DATA*/
  $auth_status = validateLogin($emailaddress, $password); 

}


/*CONFIRM EACH FIELD WAS PROCESSED*/
//trigger login field exception
try{ ### THIS BRACE IS UNMATCHED ###
  if($emailaddress == '' || $password == ''){
    throw new Exception('E-mail and password must be supplied to login.  Please try again.');
  }else{ ### THIS BRACE IS UNMATCHED ###
    /*VALIDATE FOR DATA*/
    $auth_status = validateLogin($emailaddress, $password);

    //trigger validation exception


    try{
      if(!isset($auth_status)) {
        throw new Exception('Online Banking is not available at this time.  Please try again later.');
      }
    } // closing `try`



    // catch validation exception
    catch(Exception $v) {
      echo 'message: ' . $v->getMessage();
      exit();
    } //end catch

    //This triggers the validation of authentication
    try{
      if ($auth_status == 0){
        throw new Exception('Email address and or Password does not meet our records!  Please try again.');
      }elseif ($auth_status>0){
        /*Member Session*/
        $currentMember = new Person($auth_status);

        /*Set Attributes*/
        $currentMember->memberid = $auth_status;
        $currentMember->firstname = 'Michael';
        $currentMember->lastname = 'Crawley';
        $currentMember->emailaddress = 'MichaelCrawley@sort.com';

        /*Serialize currentMember object*/
        $_SESSION['currentMember'] = serialize($currentMember);

      }
    }// End try statment

    //catch login field exception
    catch(Exception $e) {
      echo 'Message: ' . $e->getMessage();
      exit();
    }                   

?>





</div><div class="container" id="shadow">
<div>
<?php 

    include(ABSOLUTE_PATH . 'header.inc.php');

    if($auth_status == 1){
      /*AUTHENTICATION SUCCESS*/
      echo '<h4>Welcome Back, Betty!</4>' . "\n\n";
      echo '<ul>' . "\n";
      echo "\t"  . '<li><a href="' . APP_ROOT . 'onlinebanking" title="Online Banking">Online Banking</a></li>' . "\n\n";
      echo '</ul>';


    } elseif($auth_status == 0){
      /*AUTHENTICATION FAILED*/
      echo '<h4 class="error">Authentication Error!</h4>' . "\n\n";
      echo '<p>Incorrect e-mail address and/or password submitted. Please try again.</p>';
    }

?>




</div><!--End of main content-->
<?php 
    include(ABSOLUTE_PATH . 'footer.inc.php');
?>

</div>

答案 4 :(得分:0)

大多数编辑都能够突出显示匹配的大括号。

关于如何避免这些的提示:代码约定!我不会说究竟要在哪里打算什么,以及{和}是否应该在一个新的行或相同的,但总是以相同的方式。多年来,你会发现这些东西非常快 - 如果正确的话