Home / Web Development / HTML / Decision Making Statement – PHP

Decision Making Statement – PHP

Good morning to all,
The decision making statments helps in php to take some decisions in php script. It allows you to make decisions based upon the result of a conditions. Decision making statments allows you to control excecution, certain part of the php code. PHP support following conditional statment:

  • if statments
  • if…else statments
  • if..elseif.. else statments
  • Nested if statment
  • Switch statments

1. The if Statement

The syntax of the if statement is something like this:


if(condition){
true statement;
}
next statment;

In the if statement, first check if the condition is true or not. If the condition is true then excecute true staments and next staments. If the condition is false, then the true statement is not working, directly go to next statement

Example:

2. The if…else Statement

The syntax of the if statement is something like this:


if(condition)
{
true stament;
}
else
{
false stament;
}

In the if… else statment,first check the condition. First check, if the condition is true or not, if the condition is true,then excecutes the if condition part. Otherwise else part will excecutes.

Example

3. if..elseif.. else statments

The syntax of the if.. elseif…else statement is something like this:


if (condition) {
statement 1 excecute if the condition is true;
} elseif (condition) {
statement 2 executed if this condition is true;
} else {
statement 3 executed if all conditions are false;
}

In the if…elseif… else statement, first check the condition. First check,if the condition is true or not. If the condition is true, then excecute the if condition statment. If the condition is false, then go to else if staments. Check the condition in elseif statement. If the condition is true,then excecute the statment.Otherwise go to else statment.

4. Nested if statments

The syntax of the nested if statement is something like this:


if(outercondition)
{
if(innercondition)
{
true statement
}
else
{
false statments
}
}
else
{
False Statement
}

4. Switch statments

The syntax of the switch statement is something like this:


switch (n1) {
case 1:
code to be executed if n1=1;
break;
case 2:
code to be executed if n1=2;
break;
case 3:
code to be executed if n1=3;
break;
...
default:
code to be executed if n1 is different from all labels;
}

If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

Leave a Reply

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

Social Media Auto Publish Powered By : XYZScripts.com