Wednesday 30 November 2016

Chapter 4(PHP) If Else Statement,Else if Statement,Nested if Else Statement And Date Command

IF ELSE STATEMENT

If Else statement is a statement in which we can check the condition .If the condition is  true than
the if part of the program or code will run or execute and if the condition is not true than else part of the code or program is run.

PROGRAM

<html>
<body>

<?php
$d='1';
if ($d=="1")
  echo "Condition is true";

else

  echo "Condition is false";
?>

</body>

</html>

EXPLANATION
      
   In this program we take a user define variable $d and assign a value 1 to it.
  After that we use if statement and make condition if ($d=="1").It means we are comparing the             value of  $d  with 1 .Here the value assigned to $d is 1 so the condition become true and hence if         part of condition will execute and therefore   echo "Condition is true"; will execute .
  If the condition is not true than else part of condition will execute.

OUTPUT



ELSE IF STATEMENT

Else if statement is a statement in which we can check the condition .If the condition is  true than the if part of the program or code will run or execute and if the condition of if part is not true than it will compare condition  with else if part .If condition of else if part is true than it will execute that part of program .If the condition of else if part is also not true that it will execute else part of the program.

PROGRAM

<html>
<body>

<?php

$d='2';

if ($d=="1")
{
  echo "value is 1";
}
elseif ($d=="2")
{
  echo "value is 2";
}

else
{
  echo "Condition is false";
}
?>

</body>

</html>

EXPLANATION


     Here we take a user define variable $d and assign value 2 to it .After that we use if statement and make condition if ($d=="1") .After that we make else if condition like else if ($d=="2") .After that we write else statement .In this program the value of $d is 2 . The value of $d is compared with if and else if part of program .
And the part of program which is true will be executed and if both  if part and else if part of program are not true than else part of the program will be executed.
Here else if part of program is true because value of $d is 2 and in else if part we are comparing $d with 2 like ($d=="2") .And value of $d is 2 so 2=2 and hence else if part of program will be executed.

OUTPUT






NESTED IF ELSE STATEMENT

Nested if else statement means if statement inside the other if statement.
Nested if else statement is a statement in which we can check the condition .If the condition is  true than if will go inside the second if statement which we write if first if and compare it and if condition is true than it will execute second if statement and if condition id false than it will execute else part of second if statement.

PROGRAM

<html>
<body>

<?php
$d='1';
if ($d=="1")
{

$c='007';
if ($c=="007")
{
 echo "value is 007";
}
else
{
  echo "Condition is false";

}

}

else
{
  echo "Wrong Condition";
}
?>

</body>
</html>


EXPLANATION

In this program we take a user define variable $d and assign value 1 to it .
Firstly we make simple if else statement after that we write an another if else statement in the first if statement .If the if part of condition is true than if will go inside the first if statement and check the another if else statement which is written in first if statement and the part of program which is true will be executed and if the first if statement is not true than it will execute else part of program.
Here $d is equal to 1 and in our first if statement we compare value of $d with 1 .Hence the condition is true so it will go inside another if statement which is written in first .there we take a variable $c
and assign value 007 to it and compare it like if ($c=="007").If the condition is true than it will execute it and if condition is not true than it will execute else part of second if statement which is written inside first if statement.

OUTPUT


DATE COMMAND

          Date command is used to to check the current day.It is a predefined command which shows the current day .

PROGRAM

<html>
<body>

<?php

$d=date("D");
echo "Today is $d";
?>

</body>

</html>

EXPLANATION

It is a predefined command which we can use to show the current day .we can take a user define variable $d and assign date command to it like $d=date("D");
date("D") is predefined command and it should be written as it is.
And in echo we write $d which display the current day.

OUTPUT


Some Examples Of Using Date Command In IF ELSE And In  ELSE IF are given below


PROGRAM (Using date command in If Else Statement)

<html>
<body>

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

</body>
</html> 

OUTPUT



PROGRAM (Using date command in Else If Statement)

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

OUTPUT







No comments:

Post a Comment