0

The question is pretty much self-explanatory, I am having trouble how to end if statement in php.

For example,

<?php
if (argument) {
// end if statement
}
else if (different argument) {
// end if statement
}
else if (another different argument) {
// end if statement
}
else {
// do something
}
?>
9
  • 1
    Take out the else or do something, you've answered your own question...
    – Trevor
    Feb 23, 2016 at 3:09
  • For example, if the first if statement becomes true, I want the if statement not to read the entire if statement.
    – Cris Toby
    Feb 23, 2016 at 3:12
  • 1
    It won't it will exit
    – Trevor
    Feb 23, 2016 at 3:13
  • 2
    @Codexer Everything is a figment of your imagination. Including this comment. :-P
    – lc.
    Feb 23, 2016 at 3:20
  • 1
    Ok so let's get this straight for the record: With the curly brace there, the code does not do what you want. Right? (And what is it exactly you want it to do that it is not doing?)
    – lc.
    Feb 23, 2016 at 3:21

3 Answers 3

2

Consider carefully how an if condition works:

If (boolean condition) Then
    (consequent)
Else
    (alternative)
End If

When an interpreter finds an If, it expects a boolean condition ... and evaluates that condition. If the condition is true, the statements following the then are executed. Otherwise, the execution continues ... After either branch has been executed, control returns to the point after the end If.

The if statement will end if none of its conditions evaluate to true or if one of its conditions evaluate to true. The statement(s) associated with the first true condition will evaluate, and the if statement will end.

See Wikipedia's If–then(–else) for more.

1

There should not be a closing brace on line 4, but there should be one at the end of all conditional statements. The syntax is:

if (condition) 
  {
code to be executed if this condition is true;
} elseif (condition) 
  {
   code to be executed if this condition is true;
} else  
  {
code to be executed if all conditions are false;
 }

Further reference at: http://www.w3schools.com/php/php_if_else.asp

0

Change :

<?php
    if (argument) {
        // end if statement
    }
    else if (different argument) {
        // end if statement
    }
    else if (another different argument) {
        // end if statement
    else {
        // do something
    }
?>

To :

<?php
    if (argument) {
        // end if statement
    }
    else if (different argument) {
        // end if statement
    }
    else if (another different argument) {
        // end if statement
    }
    else {
        // do something
    }
?>

To use if statement, use the following syntax :

if (condition) {
    // Put your codes here
}

Another example, if you use else if :

else if (condition) {
    // Put your codes here
}

Use the curly bracket { } to contain your codes
PS: you just missed a } at the third if in your codes :)

4
  • @Codexer Ouch... Yes he did miss a bracket, and Timeout actually edited it out!! Undoing my downvote.
    – lc.
    Feb 23, 2016 at 3:13
  • 1
    @Codexer Right? And what's worse is now it's back again and this time the OP put it there. :(
    – lc.
    Feb 23, 2016 at 3:19
  • Why downvoting? (Codexer)[stackoverflow.com/users/1797425/codexer], the other answer states the same thing that he did miss a closing bracket, you need to pay attention! He has edited the mistake :( Feb 23, 2016 at 3:20
  • sorry, I mean not you, but someone :( Feb 23, 2016 at 3:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.