If else error | In if else two else not allowed?

if($user_found){
		$session->login($user_found);
		redirect("index.php");
	}else {
		$the_message = "Your password or username are incorrect";
	}else {
		$username="";
		$password="";
	}

This line is throwing error:

}else {

Here is the live link.

Parse error : syntax error, unexpected ‘else’ (T_ELSE)

what does that mean this kind of structure nort allowed:

if(){
	
}else{
	
}else{
	
}

Well how would it ever get to the last clause?

else means ‘if all else does not work, do this’.

you’re either trying to do an elseif, or you should combine your else clauses.

1 Like

I made these changes it worked:

if(isset($_POST['submit'])){
	$username = trim($_POST['username']);
	$password = trim($_POST['password']);

	///	Method to check database user
	$user_found = User::verify_user($username, $password);

	if($user_found){
		$session->login($user_found);
		redirect("index.php");
	}else {
		$the_message = "Your password or username are incorrect";
	}
}
else {
	$username="";
	$password="";
}

I brought last else synchronizing it with first If.

every if can have exactly 1 else, and the else comes immediately after that if’s final }. That’s the only way the system can pair them up properly.

1 Like

Perhaps try the PHP Manual’s elseif

<?php
if ($a > $b) {
    echo "a is bigger than b";
} elseif ($a == $b) {
    echo "a is equal to b";
} else {
    echo "a is smaller than b";
}

I prefer the alternative syntax

<?php
if ($a == 5):
    echo "a equals 5";
    echo "...";
elseif ($a == 6):
    echo "a equals 6";
    echo "!!!";
else:
    echo "a is neither 5 nor 6";
endif;
1 Like

Fair warning on that though, as the manual does point out:

if ($a > $b) {
    echo "a is bigger than b";
} else if ($a == $b) {
    echo "Still legal syntax";

but

if ($a == 5):
    echo "a equals 5";
    echo "...";
else if ($a == 6):
    echo "This will crash, as the syntax is no longer legal"
1 Like

I will remeber this in future. Thanks.

Thats the take away.

This works without a space between else and if:

Far more readable r than trying to match curly brackets :slight_smile:

<?php 

$a = isset($a) ? $a : 'IS NOT SET';
 
if ($a == 5):
    echo "a equals 5";
    echo "...";

elseif ($a == 6):
    echo "This will NOT crash, as the syntax is now legal ==> " .$a;

elseif ($a == 7):
    echo "This will NOT crash, as the syntax is now legal ==> " .$a;

elseif ($a == 8):
    echo "This will NOT crash, as the syntax is now legal ==> " .$a;

else:
    echo "This will NOT crash, as the syntax is now legal ==> " .$a;

endif;
1 Like

For that many elseif statements on the same variable value, personally I’d prefer to use switch().

1 Like

It will really help if you understood the rules; the language.

See PHP: if - Manual. It states that an if statement consists of an expression and a statement. It also says that the statement can actually be a group (the C and C++ languages call them blocks); something consisting of braces (“{}”) enclosing zero or more statements.

Then see PHP: else - Manual. Now this is an example of where the PHP manual is “friendly” but not precise. It shows the syntax of the else statement as having the braces, and it should not. It should show the else as consisting only of a statement in which the statement can optionally be a group (with braces). Also, the C and C++ manuals document the else as being an optional component of the if statement. The PHP manual documents “else” as a separate statement and implies but does not state explicitly that an else can only exist after an if.

The esleif statement just combines the else and if; I am not sure but I assume the only difference is that there is no space in elseif.

If you understand that syntax then you can understand the proper syntax for doing what you asked about and you will better understand if and else in the future.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.