PHP

Conditional Statement

Conditional statements are the set of commands used to performed different  actions based on different conditions.
In PHP we have the following

  • if
  • if else
  • else if
  • switch

if statement
if structure is used for conditional execution of code segment.
Syntax-

if(expr)
{
statements
}

Example-

<?php
if($c>$d)
{
echo "c is greater than d";
}
?>

In the above example ,only if the condition “$c>$d” is true, the message “”c is greater than d” is displayed.
Else Statement
The conditional statement  “else”  is used as extension of “if” statement. If the condition fails then it executes another statements under the “else” condition.
Syntax-

if(expr)
{
statements
}//true
else
{
statements
}//false

Based on the result of expressions, the statements are executed.
Example –

<?php
$c=10;
$d=20;
if($c>$d)
{
echo "c is greater than d";
}
else
{
echo "d is greater than c";
}
?>

Result – d is greater than c
In the above example in the if condition “$c>$d” is true then the message “c is greater than d” is displayed , else the message “d is greater than c” is displayed.
Else if Condition
else if condition is used as extension of “if” structure if the condition fails then it executes another “If” condition to execute the code segment under the “else if” statement.
Syntax –

if(expr 1)
{
statements
}// true
elseif(expr 2)// false
{
statements
}// true
else
{
statements
}// false

Based on the failure “expr 1” condition, “expr 2” is checked and then the statements are executed.
Example –

<?php
$c=10;
$d=10;
if($c>$c)
{
echo "c is greater than d";
}
elseif($c==$d)
{
echo "c is equal to d";
}
else
{
echo "d is smaller than c";
}
?>

Output- c is  equal  to d
In the above example the if the condition “$c>$d” is true then the message “c is greater than d” is displayed, else the condition in the else if that is “$c==$d” is evaluated if it is true the message “c is equal to d” is displayed otherwise d is smaller than c is displayed.
Switch Statement
The switch case statement is used to compare a variable or expression values based on which a set of code executed.
Syntax-

switch(variable or expression)
{
case (value 0)
{
statements
}
case (value 1)
{
statements
}
..............
case (value n)
{
statements
}
default
{
statements
}
}

Example-

<?php
$c=3;
switch($c)
{
case 0:
echo "value of $c=0<br>";
break;
case 1:
echo "value of $c=1<br>";
break;
case 2:
echo "value of $c=2<br>";
break;
default:
echo "value of $c=Default value <br>";
break;
}
?>

Output-  value of 3 = Default value
In the above example based on the value of $c the messages are displayed of that particular case which matches. The default case accepts anything not matched by other cases, so the value “3” displays the default message.

Recommended Tutorials for you


Related posts

How to get Browser Information In PHP

Anuj Kumar

Get City Country By IP Address in PHP

Anuj Kumar

PHP Constants

Anuj Kumar

How to change Password in php

Anuj Kumar

Sorting Arrays in PHP

Anuj Kumar

Understanding of php.ini

Anuj Kumar

Server Side form Validation in PHP

Anuj Kumar

How to insert data into MySql using PHP

Anuj Kumar

COOKIES IN PHP

Anuj Kumar

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.