Conditional Statements

Posted by tutor | Posted in PHP Tutorial | Posted on 04-08-2010-05-2008

0

Conditional statements are used to evaluate some conditions and perform different actions based on the outcome.

You can use conditional statements in your code to do this.

PHP provides the following conditional statements:

  • If statement – execute the piece of code only if a specified condition is true.if (condition) code to be executed if condition is true;
  • If…else statement – this statement executes a two way condition, where the true part does something else and the false part executes something else.if (condition)
    code to be executed if condition is true;
    else
    code to be executed if condition is false;
  • If…elseif….else statement – if there are multi-way condition, where there are several blocks of code to be executed based on different conditions.if (condition)
    code to be executed if condition is true;
    elseif (condition)
    code to be executed if condition is true;
    else
    code to be executed if condition is false;
  • Switch statement – this statement is used to select one of many blocks of code to be executedswitch (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;
    }

Write a comment

Twitter Users
Enter your personal information in the form or sign in with your Twitter account by clicking the button below.