PHP Theory
If...Else Statement
if is a conditional control statement. If condition is ture then the statements inside the body of "if " are executed or then inside body of "else" are executed.
If condition returns false then the statements inside the body of “if” are skipped and the statements inside body of “else” are executed.
If Else Statement
If Else Statement
The if-else statement is also used to test the conditions.If the condition is true, then if block is executed.If the condition is false, then the else block is executed.
Syntax of If Else Statement |
---|
if(condition) { //Code to be executed if condition is true } else { //Code to be executed if condition is flase } |
Nested if else statement
Nested if else statement
The nested if statement means when there is an if statement inside another if statement. The inner if statement to be executes when the condition of outer if statement should be true.
Syntax of Nested if...else statement |
---|
if (condition1) { //Statement(s) if (condition2) { //Statement(s) } else { //Statement(s) } } else { //Statements(s) if (condition3) { //Statement(s) } else { //Statement(s) } } |
Else - If Statement
Else - If Statement
The if-else-if statement is used to check the multiple conditions. It is also known as "if..else..if" ladder. This statement have only one "if" and "else" statements and multiple "else if" statements. If any one of the if condition is true, the statement related with that if is executed, and the remainder of the ladder is avoided. If none of the condition is true, then the final else statement will be executed.
Syntax of else if statement |
---|
if (condition 1) { //Code to be executed if condition 1 is true } else if (condition 2) { //Code to be executed if condition 2 is true } else if(condition 3) { //Code to be executed if condition 3 is true } else if { //Code to be executed if none of the above condition is true } |