Php – If..Elseif..Else Conditional Statements

RSS
FACEBOOK
FACEBOOK
PINTEREST
PINTEREST
LINKEDIN
INSTAGRAM

[rps-include post=6522]

Php provides ability to write applications according to decision making with given statements or comparative tests. We can test given situations and conditions and dictate application execution. For example we want to check given integer value and print the positiveness and negativeness. Here we need to check this situation with if-elseif-else conditional statements.

If Statement

If statement will check given condition and if the condition is true if statement code block will be executed, if not it will be simply skipped. Here the if statement syntax.

I think the best way to understand the if statement is doing simple and clear example. We have a integer variables named $temp which holds temperature and we want to check whether the temperature if higher then 0 . If so we will print Temperature is above 0 .

Here most important part is $temp>0 where $temp value will be compared with comparison operator > and an boolean result like true will be returned.

If..Elseif Statement

In previous example we have check only one condition but real world is different and more complex than that. We generally check multiple conditions in a single if..elseif statement. This statement will start with if and then for each condition we will add an elseif statement like below.

In this example we will check student name which is hold variable named $student . If the condition met we will print the name.

As we expect last elseif statement condition will match and print Ali . Thre is no restriction about the count of additional elseif statements.

If..Elseif..Else Statement

We can precisely specify the conditions and mathes. But what if we want to specify completely unmatched situation and run execute some code. We can use else statement at the end of the if..elseif statement block and this means run if none of above conditions matched.

In this example we will check $count and if none of the statements mathed we will print empty .

[rps-include post=6522]

RSS
FACEBOOK
FACEBOOK
PINTEREST
PINTEREST
LINKEDIN
INSTAGRAM

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)