Check out the Latest Articles:

This is one of the most important tutorial for php programming is dealing with Conditional Statements.

Let me first illustrate how does this work. For instance whenever you need to check some conditions like whether the value of ‘a’ is greater than ‘b’ or vice versa you will be in need to use statements as if, if else, elseif, switch each have their own specific use and definite purpose to be fulfilled.

$a = 10; $b = 5;
if ($a > $b)
{
echo " A is Greater ";
//The controller enters this block only if true
// which in turn executes all the statements in this block
}

We got 4 conditional statements in PHP and most of other programming languages to have the same operators even the syntax is alike few programming languages might differ but the working is same.

Let me give you the syntax for them

1. if

if(Condition)
{

}

Example

if ($passwd==$myvalue)
{
echo "Password correct ";
}

2. if else

if(Condition)
{

}
else
{

}

Example

if ($passwd==$value)
{
echo " Password correct";
}
else
{
echo " Password Incorrect";
}

3.elseif

if(Condition)
{

}
else if (Condition)
{

}
else if(Condition)
{

}
else
{

}

Example

if ($a>$b AND $a>$c)
{
echo "A is Greater";
}
else if ($b>$a && $b>$c)
{
echo "B is Greater";
}
else if ($c>$a && $c>$b)
{
echo "C is Greater";
}
else
{
echo " All Values are equal";
}

4. Switch

switch( expression )

{

case expression:

case expression:

case expression:

default:

}

Example

switch (add)
{

case add: $sum=$a+$b;
echo $sum;

case minus: $sub=$a-$b;
echo $sub;
default: echo " Enter valid options like add or minus";
}

By these example I think you probably got the idea how to use and when to use such type of conditional statements, well effective utilization of these statements can solve any many logical problems and help create better algorithms.

Related posts:

  1. Learn PHP in 7 Days
  2. PHP Syntax, Variables, Echo Statement – Learn PHP in 7 Days – Day #1
  3. Using Operators in PHP – Learn PHP in 7 Days – Day #2
  4. Using Comments in PHP – Learn PHP in 7 Days – Day #3
  5. MS DOS Tutorials – How to Create, Append and View Files and Move Files and Directory


  1. [...] Day #4 – Using Conditional Statements in PHP [...]