else if else

Location of the Videos
Post Reply
tarnos12
Posts: 16
Joined: Thu Dec 26, 2013 12:42 pm

else if else

Post by tarnos12 »

Hey following videos I was trying to change something but it didnt work, there is a code:

Code: Select all

if(!$_POST['password'] || !$_POST['password2'])
{
  print "You did not enter a password<br>";
  echo "<A href='register.php'>Go back</a>";
  exit;
}
else if($isplayer3)
{
  print "There is already a player of that name";
  echo "<A href='register.php'>Go back</a>";

else (strlen($player)>21 || strlen($player)<5)

 print "The name you specified must be between 5-21 characters long<br>";
 echo "<A href='register.php'>Go back<a>";
 exit;
I am not asking you to make it for me or anything, but give me some info what is wrong here. I guess that when I start with "if" then it follow up with "else if" then "else" but is there any way to add more to it? I mean more "elses" so I can keep checking more and more at once or is there a better way? I can see in that code name/passwords are together if/else if/else and then there is an exit and another else for email. I will try to figure out how that works and maybe I will manage.

If password1 does not match pass2

echo pass1 does not match pass2

else if check if password is not too long (if password1 match password2 then there is else if )
echo password is too long

else check if password is not too short(if else if fails too which means in this case password is not too long then there is another else)
echo password is too short(and lastly if password match pass2 its not too short or too long then its all good but I might want to add more else, which give me an error of unexpected else.)

exit


and now what? is it all I can do? I want to add another line depending on what might happen. I know its not necessary and I could just follow video and put all in one place but I want to try different ways. Of course before someone answer me I will try to find solution by myself. If I do I will post here again. Thanks :)

Sorry if this post is messed up ^^

@edit:

when I play with that else and change it, for some reason I keep getting syntax error, unexpected 'else' in different places when I add/delete }{ to make it work :| I am still trying.
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: else if else

Post by Xaos »

You can have an unlimited number of "else if". But it has to be

If(){ }
elseif() { }
elseif() {}
elseif() { }
else{ }

etc.
Zester
Posts: 53
Joined: Sat Mar 03, 2012 11:33 pm

Re: else if else

Post by Zester »

This last else should be a else if.
else is used to execute code the defult code, if you want to have comparative statements "(strlen($player)>21 || strlen($player)<5)" you end a if or in your case if else
comparative statements

else (strlen($player)>21 || strlen($player)<5)
to
else if (strlen($player)>21 || strlen($player)<5)
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: else if else

Post by MikuzA »

hello,

You can't check stuff with 'else', you'll have to add the else if in that last check.
But just to keep the security tight, add an extra 'else' in the end with exit; or something.

If all 'IF' criterias are FALSE, then it gets triggered instead of it passing through the if-else-statements.

so,

Code: Select all

else if (strlen($player)>21 || strlen($player)<5) {
     print "The name you specified must be between 5-21 characters long<br>";
     echo "<A href='register.php'>Go back<a>";
     exit;
}
else {
     exit;
}
 
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: else if else

Post by Xaos »

You can pretty much do anything with if-else if-else, I think they're the most powerful tool in any programming language and the most useful :D
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: else if else

Post by MikuzA »

oh lol, I didn't realise the answer to this problem was already replied..

Oh well, I'll put it on Monday Mornings-account, last bloopers of 2013..
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: else if else

Post by Chris »

Code: Select all

$condition = 'something' === 'something'; 
Generally understood statement, can't go wrong.

Code: Select all

// common
if ($condition) {
   // do something
} elseif (!$condition) {
   // do something else
} else {
  // this most likely wont happen in this case
} 
All about indentation and line endings, not recommended but does work.

Code: Select all

// can go wrong
if($condition)
   // do something
elseif(!$condition)
  // do something else
else
  // this most likely wont happen in this case         
Good for view logic code as it is more readable

Code: Select all

// more readable
if($condition) :
   // do something
elseif(!$condition) :
  // do something else
else :
  // this most likely wont happen in this case
endif; 
Shorthand:

Code: Select all

echo $condition ? 'Condition was met' : !$condition ?  'Condition was not met' : 'This won\'t happen in this case';
Fighting for peace is declaring war on war. If you want peace be peaceful.
tarnos12
Posts: 16
Joined: Thu Dec 26, 2013 12:42 pm

Re: else if else

Post by tarnos12 »

Thats great, this will help me alot. Thanks :)

@Edit:

okay so I had to delete this whole post since it was a big mess :) I watched video 3b few times and I got the hang of it...I understand now where was the problem...It was all because I messed up with { }

Once I do what I am trying to acomplish I will post the code so you can check if its actually good or not :)

Its great! I really did it ^^

Code: Select all

    <?php
    include 'connect.php';
    ?>

     <?php
    $player=$_POST['player'];
    $password=$_POST['password'];
    $pass2=$_POST['pass2'];
    $player=strip_tags($player);
    $email=$_POST['email'];
    $email=strip_tags($email);

    if ($email == "")
    {
     echo "You didn't enter a email address!<br>";
     echo " <A href='register.php'>Go back</a>";
        exit;
        }
    if ($password==$pass2)
    {
     
      $isplayer="SELECT * from players where name='$player'";
      $isplayer2=mysql_query($isplayer) or die("Could not query players table");
      $isplayer3=mysql_fetch_array($isplayer2);

      if(!$_POST['password'] || !$_POST['pass2'])
      {
         print "You did not enter a password";
        echo " <A href='register.php'>Go back</a><br>";
        exit;
      }
      else if($isplayer3)
      {
         print "There is already a player of that name<br>";
         echo " <A href='register.php'>Go back</a>";
        exit;
      }

      else if (strlen($player)>21 || strlen($player)<5)
      {
        print "Your password must be 5-21 characters long<br>";
        echo " <A href='register.php'>Go back</a>";
        exit;
      }

      else
      {
        $isaddress="SELECT * from players where email='$email'";
        $isaddress2=mysql_query($isaddress) or die("not able to query for password");
        $isaddress3=mysql_fetch_array($isaddress2);
        if($isaddress3)
        {
          print "There is already a player with that e-mail address<br>";
          echo " <A href='register.php'>Go back</a>";
        exit;
        }
        else
        {
            $password=md5($password);
           
     $SQL = "INSERT into players(name, password, email, level, exper) VALUES ('$player','$password','$email','1','0')";
          mysql_query($SQL) or die("could not register");
         
          print "Thank you for registering.";
       
        }
      }
    }

    else
    {
      print "Your password didn't match or you did not enter a password<br>";
       echo " <A href='register.php'>Go back</a>";
        exit;
    }
    echo "<br><A href='login.php'>Login Page</a>";
    ?>
It might be simple but I learned something for sure, really great. Ofc this whole code is not made by me, I just changed something really small :| but still thats something ^^ At last I got some understanding of if else if { }

@edit:

I have another question, what if I wont use "exit" ? after every else if I have created? Will that make it so code will check all else if? and give me error message for each of them? What I want to try is to show all errors at once, like username is already in use + password do not match etc.
Post Reply

Return to “Older Browser MMO Videos”