Join TalkersCode Now !

Get Latest Tutorials And Links On Web Development
And Get Existing Offers Via Email

PHP Decision Making

PHP Decision Making

PHP Decision Making statements are if, else, elseif and switch these statements are used in making decisions.



if..else statements

if..else statements is used where you want to execute a set of code when a condition is true and another if the condition is not true


Syntax


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


Example of if..else

$a=10;
if ($a==10)
{
  echo "Yes the value is 10"; 
}
else
{
  echo "No the value is not 10"; 
}



elseif statements

elseif statements is used where you want to execute a some code if one of several conditions are true use the elseif statement


Syntax


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
}


Example of elseif

$a=10;
if ($a==20)
{
  echo "Yes the value is 20"; 
}
elseif($a==10)
{
  echo "Yes the value is 10"; 
}
else
{
  echo "No you are wrong"; 
}



switch statements

switch statements is used where you want to execute one block of code out of many.


Syntax


switch (value)
{
case value1:
  code to be executed if value = value1;
  break;  

case value2:
  code to be executed if value = value2;
  break;

default:
  code to be executed
  if value is different 
  from both value1 and value2;

}


Example of switch

$a=10;
switch ($a)
{
case 10:
  code to be executed if $a=10;
  break;  

case 20:
  code to be executed if $a=20;
  break;

default:
  code to be executed
  if value is different 
  from both value1 and value2;

}
PrevNext

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email

Are You Starting A New Blog
Do You Want Best Web Hosting?

Try World's Best Web Hosting Provider BlueHost
At Just $3.95/mo

Hurry Up! Limited Time Offer

Yes, I Want To Check Out BlueHost

Check The World's Best SEO And All-In-One Marketing Toolkit
For Digital Marketing Professionals SEMRUSH

Check Out SEMRUSH