Showing posts with label PHP Conditional Statements. Show all posts
Showing posts with label PHP Conditional Statements. Show all posts

Monday, 5 August 2019

How to use if else condition in php

Introduction to if else condition in php

php programming like other languages have the conditional statement processing also.Conditional statement add intelligence to our scripts. This gives us the functionality to perform various command based on various conditions
We will be working on if/then/else statement in this post i.e if else condition in php
The basic format for the if/then/else conditional statement processing is
if condition
{
commands
}
else
{
commands

}
A Example would be
if ($name = “” )
{
echo “name is empty”’;
}
else
{
echo “name is $name”;
|
It is also possible to create compound conditional statements by using one or more else if (elif) clauses. If the first condition is false, then subsequent elif statements are checked. When an elif condition is found to be true, the statements following the associated then statement are executed.
if condition
{ commands}
elif condition
{commands}
else
{ command}
Example
if ($x < “1000”) {
echo “you are awesone!”;
} elseif ($x < “2000”) {
echo “You are good!”;
} else {
echo “You are bad!”;
}

Algorithm for if else in php

The if statement uses the exit status of the given condition
if test condition
{
commands (if condition is true)
}
else
{
commands (if condition is false)
}
if statements may be nested:
if  conditions
{commands}
else if conditions
{commands}
else
{commands}

Various operators which are used in condition

OperatormeaningExampleOperator Result
==Equal$a == $bReturns true if $x is equal to $y
===Identical$a === $bReturns true if $x is equal to $y, and they are of the same type
!=Not equal$a != $bReturns true if $x is not equal to $y
<>Not equal$a <> $bReturns true if $x is not equal to $y
!==Not identical$a !== $bReturns true if $x is not equal to $y, or they are not of the same type
>Greater than$a > $bReturns true if $x is greater than $y
<Less than$a < $bReturns true if $x is less than $y
>=Greater than or equal to$a >= $bReturns true if $x is greater than or equal to $y
<=Less than or equal to$a <= $bReturns true if $x is less than or equal to $y

And and OR operator
we can test multiple test condition with && and || operator

&& – this stand for and condition
($1 == yes)  && ( $2 ==yes)
So both the condition need to be true for this whole expression to be true
|| – This stand for or condition
($1 == yes)  || ( $2 ==yes)
Only one condition need to be true for this whole expression to be true.

Thursday, 4 September 2014

Conditional Statements In PHP

The if, elseif ...else and switch statements are used to take decision based on the different condition.
You can use conditional statements in your code to make your decisions. PHP supports following threedecision making statements:
  • if...else statement - use this statement if you want to execute a set of code when a condition is true and another if the condition is not true
  • elseif statement - is used with the if...else statement to execute a set of code if one of several condition are true
  • switch statement - is used 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.

The If...Else Statement

If you want to execute some code if a condition is true and another code if a condition is false, use the if....else statement.

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>