Jump to content


Photo

How deep nested elseif can manage?


  • Please log in to reply
2 replies to this topic

#1 bilis_money

bilis_money
  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 08 August 2006 - 12:59 PM

i'm trying to make a nested elseif

ok, here are the codes,
//check for empty fields
	if($user == ''){
		$COerr_msgs->err_msgs('register1');
	} else if($pass == ''){
		$COerr_msgs->err_msgs('register1');
	} else {
		//compare username into database
		if($user_new == $user_old) {
			if($pass_new == $pass_old) {
				//check activated if set if set then proceed to authorized page.
				if(activate_sta == '1') {
					include "authorize_page.php";
				} else {
					$COerr_msgs->err_msgs('login1');
				}
			} else {
				$COerr_msgs->err_msgs('register2');
				exit();
			}
		} elseif($user_new != $user_old) {
			$COerr_msgs->err_msgs('activated2');
			exit();
		}
	}

I don't how deep an elseif condition can manage.
but i think i have experiencing confusion because
the first else condition of the elseif does't threw me
the right error message, but instead it mere display nothing?
just a blank page.

I don't know if i had mistake with that codes, and i don't know
if there are some hidden rules about nested elseif.

ok, i'll try to review my codes while i'm awaiting for your comments.

thanks in advance.





#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 08 August 2006 - 01:33 PM

There's no benefit to nesting two tests that both cause identical results. This works fine:

if (($user == '') || ($pass == '')) {
    $COerr_msgs->err_msgs('register1');
}

Legend has it that reading the manual never killed anyone.
My site

#3 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 08 August 2006 - 02:39 PM

I can never understand why people do not align their opening and closing braces! It makes it so much easier to spot errors.

Example:
             if($user == '')
             {
                         $COerr_msgs->err_msgs('register1');
             }
             else if($pass == '')
             {
                         $COerr_msgs->err_msgs('register1');
             }
             else
             {
                         //compare username into database
                         if($user_new == $user_old)
                         {
                                      if($pass_new == $pass_old)
                                      {
                                                   //check activated if set if set then proceed to authorized page.
                                                   if(activate_sta == '1')
                                                   {
                                                                include "authorize_page.php";
                                                   }
                                                   else
                                                   {
                                                                $COerr_msgs->err_msgs('login1');
                                                   }
                                      }
                                      else
                                      {
                                                   $COerr_msgs->err_msgs('register2');
                                                   exit();
                                      }
                          }
                          elseif($user_new != $user_old)
                          {
                                      $COerr_msgs->err_msgs('activated2');
                                      exit();
                          }
             }

Identical to your code but so much easier to read! Well I think so anyway, YMMV.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users