Posts Tagged ‘php programming’

PHP If … Else Conditional Statements

The if, elseif …else and switch statements are used to take decision based on the different condition.

Conditional statements are used to perform different actions based on different conditions.

 

PHP 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.

PHP supports following decision making statements:

  • if statement – executes some code only if a specified condition is true
  • if…else statement – executes some code if a condition is true and another code if the condition is false
  • if…else if….else statement – selects one of several blocks of code to be executed
  • switch statement – selects one of many blocks of code to be executed

PHP – The if Statement

The if statement is used to execute some code only if a specified condition is true.

Syntax

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

 
The example below will output "Have a good day!" if the current time is less than 20: Example:
<?php
  $t=date("H");
  if ($t<"20")
    {
    echo "Have a good day!";
    }
?>

Output Result:

Have a good day!

 

The If…Else Statement

Use the if….else statement to execute some code if a condition is true and another code if the condition is false.

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

Example

The following example will output "Have a nice weekend!" if the current day is Friday, otherwise it will output "Have a nice day!":

<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
  echo "Have a nice weekend!"; 
else
  echo "Have a nice day!"; 
?>
</body>

</html>

If more than one line should be executed if a condition is true/false, the lines should be enclosed within curly braces:

<html>

<body>
<?php
$d=date("D");
if ($d=="Fri")
  {
  echo "Hello!<br />"; 
  echo "Have a nice weekend!";
  echo "See you on Monday!";
  }
?>
</body>
</html>

The ElseIf Statement

If you want to execute 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

The following example will output "Have a nice weekend!" if the current day is Friday, and "Have a nice Sunday!" if the current day is Sunday. Otherwise it will output "Have a nice day!":

<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
  echo "Have a nice weekend!"; 
elseif ($d=="Sun")
  echo "Have a nice Sunday!"; 
else
  echo "Have a nice day!"; 
?>
</body>
</html>

The Switch Statement

If you want to select one of many blocks of code to be executed, use the Switch statement.

The switch statement is used to avoid long blocks of if..elseif..else code.

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

}

Example

The switch statement works in an unusual way. First it evaluates given expression then seeks a lable to match the resulting value. If a matching value is found then the code associated with the matching label will be executed or if none of the lables match then statement will will execute any specified default code.

<html>
<body>
<?php
$d=date("D");
switch ($d)
{
case "Mon":
  echo "Today is Monday";
  break;
case "Tue":
  echo "Today is Tuesday";
  break;
case "Wed":
  echo "Today is Wednesday";
  break;
case "Thu":
  echo "Today is Thursday";
  break;
case "Fri":
  echo "Today is Friday";
  break;
case "Sat":
  echo "Today is Saturday";
  break;
case "Sun":
  echo "Today is Sunday";
  break;
default:
  echo "Wonder which day is this ?";
}
?>
</body>
</html>

Next Chapter »

 

Designed for Mytoptutorials.com