Friday 11 December 2015

Conditional statement in PHP

Conditional statement in PHP

  PHP supports different types of conditional statements:-
The if Statement
In if Statements Output will appear when only Condition must be true.
The if-else Statement
if-else statements allows you to display output in both the condition(if condition is true display some message otherwise display other message).
The if-elseif-else Statement
The if-else-if-else statement lets you chain together multiple if-else statements, thus allowing the programmer to define actions for more than just two possible outcomes.
The switch Statement
The switch statement is similar to a series of if statements on the same expression.

if statement in PHP
  
The if Statement is the simplest conditional statements.
In if  Statements Output will appear when only Condition must be true.
If Statement works  much like the English language statement, “if X happens, then do Y.”

Write a program to check even number.(Number entered by user)
 <?php
if(isset($_GET['save']))
{
  if($_GET['n']%2==0)
  {
  echo $_GET['n']." is even number";
  }
}
?>
<body>
<form method="get">
Enter Your number<input type="text" name="n"/><hr/>
<input type="submit" value="check number" name="save"/>
</form>
</body>

if else statement in PHP
 The if statement is quite basic because in If statement output will display when condition must be true, if condition is false it display nothing(blank).
But if-else statements allows you to display output in both the condition(if condition is true display some message otherwise display other  message).
In English, this statement would read, “if X happens, do Y; otherwise do Z”.

check if given number is greater than 0, then show number is 
positive else number is negative.
<?php
$num=$_POST['n'];
if($num>0)
  {   echo $num." is positive number";   }
else
  {   echo $num." is negative number";   }
?>
<body>
<form method="post">
Enter Your number<input type="text" name="n"/><hr/>
<input type="submit" value="check number"/>
</form>
</body>

nested if else in PHP
 The if-else-if-else statement lets you chain together multiple if-else statements, thus allowing the programmer to define actions for more than just two possible outcomes.
Check given character is vowel or consonant.
<body>
<form method="post">
  Enter Your number<input type="text" name="ch"/><hr/>
  <input type="submit"/>
</form>
</body>

<?php
$char=$_POST['ch'];
if($char=="a")
  {   echo $char." is vowel";   }
elseif($char=="e")
  {   echo $char." is vowel";   }
elseif($char=="i")
  {   echo $char." is vowel";   }
elseif($char=="o")
  {   echo $char." is vowel";   }
elseif($char=="u")
  {   echo $char." is vowel";   }
else
  {   echo $char. "is consonent";   }
?>

switch statement in php
The switch statement is similar to a series of if statements on the same expression.
The following two examples are two different ways to write the same thing, one using a series of if and else-ifstatements, and the other using the switch statement.

Check whether the value matches with the given value or not.
<?php
$i=5;
switch ($i)
{
  case 0: echo "i equals 0";
       break;
  case 1: echo “i equals 1";
       break;
  case 2: echo "i equals 2";
       break;
  default: echo "i is not equal to 0, 1 or 2";
}
?>
Output : 5 is not equal to 0, 1 or 2