Conditional Statements



Conditional Statements: Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. In PHP we have the following conditional statements:
  • if statement - use this statement to execute some code only if a specified condition is true
  • if...else statement - use this statement to execute some code if a condition is true and another code if the condition is false
  • if...else if....else statement - use this statement to select one of several blocks of code to be executed
  • switch statement - use this statement to select one of many blocks of code to be executed

 

Syntax of The if Statement:

if (condition) {
code to be executed if condition is true; }

Syntax of The if...else Statement:

if (condition)  {
code to be executed if condition is true; }
else     {
code to be executed if condition is false; }

Syntax of The if...else if....else Statement:

if (condition) {
code to be executed if condition is true; }
else if (condition) {
code to be executed if condition is true; }
else {
code to be executed if condition is false; }

Syntax of The PHP Switch Statement:

switch (n){
case label1:
            code to be executed if n=label1;
            break;
case label2:
            code to be executed if n=label2;
            break;
default:
  code to be executed if n is different from both label1 and label2;
}

Loading
Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Flying Twitter Bird Widget By ICT Sparkle