Conditional Statement in PHP:-

15

It is used to solve a condition-based problem using If and Else Statement.
If Statement will be executed when the condition will be true and else Statement will be executed when
condition is false.
Syntax of the conditional statement:-
1) Simple If:-
if(condition)
{
     Statement;
}
WAP to print only even numbers?
<?php
$num=4;
if($num%2==0)
echo "Number is ",$num;


?>
2)  If--Else:-

if(condition)
{
     Statement;
}
else
{
       Statement;
}
WAP to check the greater number?
<?php
$a=100;
$b=20;
if($a>$b)
echo "$a is greater";
else
{
echo "$b is greater";
}
?>

WAP to check two digits and three-digit numbers?

<?php
$num=12;
if($num>=10 && $num<100)
 echo "Two-Digit Number";
else
{
   if($num>=100 && $num<1000)
    echo "Three Digit Number";
}
?>
WAP to check two-digit negative numbers and positive numbers?
3) Nested if-else :-
Using this we can write more than one if-else statement using nested sequence, it contains collection of one outer if-else and multiple inner if-else.
if(condition)
{
       if(condition)
        statement
      else
        statement
  }
else
{
  if(condition)
        statement
      else
        statement
}  
        
WAP to check the greatest number?
    <?php
$a=100;
$b=200;
$c=300;

if($a>$b)
{
    if($a>$c)
    echo "a is greatest";
    else
    echo "c is greatest";

}
else
{
if($b>$c)
echo "b is greatest";
else
echo "c is greatest";


}

?>

WAP to check the greatest using four different numbers?

<?php
$a=1000;
$b=2000;
$c=30;
$d=400;
if($a>$b)
{
    if($a>$c)
    {
    if($a>$d)
    echo "a is greatest";
    else
    echo "d is greatest";
    }
   
      else
     {
    if($c>$d)
    {
    echo "c is greatest";
    }
    else
    {
    echo "d is greatest";
    }
    }
   


}
else
{
     if($b>$c)
     {
      if($b>$d)
      {
      echo "b is greatest";
      }
      else
      {
      echo "d is greatest";
      }
     }     
     else
     {
      if($c>$d)
      {
      echo "c is greatest";
      }
      else
      {
      echo "d is greatest";
      }
     }
     


}


?>


WAP to increase the salary of an employee from 500 if entered salary is less then 10000 ,increase 1000 if salary will be less then 20000 otherwise increase 2000

....................................................................................................................................................

<?php

$salary = 25000;


if($salary<10000)
{
$salary = $salary +500;
}
else
{
    if($salary<20000)
    $salary=$salary+1000;
    else
    $salary=$salary+2000;
}
echo "salary is ",$salary;
?>
Q) WAP to check that the assigned number is one digit,two-digit,three-digit or above this using nested if-else(not use and operator)?
<?php
$num=4500;
if($num<10)
echo "one digit";
else
{
    if($num<100)
    echo "two-digit";
    else
    {
       if($num<1000)
    echo "three-digit";
       else
        echo "above three-digit";

    }
 }
?>
Ladder if--else:-
It will work step by step means first execute if condition, if it is true then if block will be executed, if it will be false then else if block will be executed, else if the block can be multiple if all else if block will be false then finally else block, will be executed.
if(condition)
statement
elseif(condition)
statement
elseif(condition)
statement
else
statement
WAP to calculate the greatest number using a ladder if-else?
<?php
$a=100;
$b=2000;
$c=300;
if($a>$b && $a>$c)
echo "a is greatest";
else if($b>$c)
echo "b is greatest";
else
echo "c is greatest";
?>
Multiple If:-
Using this we can combine multiple if statements, it is used to solve multiple conditions with multiple results.
Syntax:-
if(condition)
statements;
if(condition)
statements;

....
WAP to check divisibility of number that it is divisible by 3,5 and 9 with all combinations?
ASSIGNMENT of nested if-else:-
WAP to find the max number using four different numbers?

Solution:-
<?php
$a=1000;
$b=2000;
$c=3000;
$d=400;
if($a>$b)
{
    if($a>$c)
    {
          if($a>$d)
             echo "a is greatest";
          else
          echo "d is greatest" ;
    }
    else
    {
         if($c>$d)
          echo "c is greatest";
         else
          echo "d is greatest";
    }
 }
else
{
if($b>$c)
{
if($b>$d)
echo "b is greatest";
else
echo "d is greatest";
}

else

{
if($c>$d)
echo "c is greatest";
else
echo "d is greatest";
}
}
?>
WAP to check vowel, consonant using nested without a logical operator?
WAP to display yes, no, cancel when user assign 'y', 'n', and 'c' into a variable?
WAP to check age group if age limit <18 then group 'A' , age limit <35 group B, age limit <60 group c and above >60 then group d?

WAP to create a mark sheet using five different subjects with the following conditions?
1)  all subject marks should be 0 to 100?
2)  If only one subject mark is <33 then the student will supply.
3)  If all subject marks are>33 then student pass and display division with percentage?
4)  if the minimum of two subject marks is <33 then the student will fail.
5)  if the student is suppl and mark is >28 then five bonus marks will be added then the student will pass by grace and grace subject name.
6) display distinction subject name
7) display suppl subject name

The solution of complete program:-
<?php
$phy = 96;
$chem=89;
$math=98;
$eng=35;
$hin=30;
if(($phy>=0 && $phy<=100) && ($chem>=0 && $chem<=100) &&  ($math>=0 && $math<=100) &&  ($eng>=0 && $eng<=100) &&  ($hin>=0 && $hin<=100))
{

    $c=0;
    $grace=0;
    $sub="";
    $d="";
    if($phy<33)
    {
    $grace=$phy;
    $sub = $sub." PHYSICS ";
    $c++;
    }
   
    if($chem<33)
    {
    $grace=$chem;
    $sub = $sub." CHEMISTRY ";
    $c++;
    }
    if($math<33)
    {
    $grace=$math;
    $sub = $sub." MATH ";
    $c++;
    }
    if($eng<33)
    {
    $grace=$eng;
    $sub = $sub." ENGLISH ";
    $c++;
    }
    if($hin<33)
    {
    $sub = $sub." HINDI ";
    $grace=$hin;
    $c++;
    }
    if($phy>=75)
    $d = $d. " PHYSICS ";
    if($chem>=75)
    $d = $d. " CHEMISTRY ";
    if($math>=75)
    $d = $d. " Maths ";
    if($eng>=75)
    $d = $d. " ENGLISH ";
    if($hin>=75)
    $d = $d. " HINDI ";

    if($c==0 || ($c==1 && $grace>=28))
    {
      if($grace>0)
      {
      $per = ($phy+$chem+$math+$eng+$hin+(33-$grace))/5;
      echo "pass by Grace in $sub  <hr> grace marks is ".(33-$grace)."<hr>";
      }
      else
      {
     $per = ($phy+$chem+$math+$eng+$hin)/5;
     }
     if($per>=33 && $per<45)
     {
      echo "Pass in third division percentage is $per %";
     }
     else if($per<60)
     {
      echo "Pass in second division percentage is $per %";
     }
     else
     {
      echo "first division percentage is $per %";
     }
     if($d!="")
     {
      echo "<hr>Distinction Subject Name is $d";
     }
    }
    else if($c==1)
    {
    echo "suppl in $sub";

    }
    else
    {
        echo "failed in $sub";
    }
}
else
{
echo "all subect marks should be 0 to 100";
}

?>
Tags

Post a Comment

15Comments

POST Answer of Questions and ASK to Doubt

  1. nayansi purwar
    batch:5:00 pm;

    60 then group d*/

    $age=70;
    if($age<18)
    echo" you are in group A";
    else
    {
    if($age<35)
    echo "you are in group B";
    else
    {
    if($age<60)
    echo" you are in group C";
    else
    echo"you are in group D";
    }
    }

    ReplyDelete
  2. Kritika Barod


    =10&&$a<=100)
    echo "two digit positive number";
    else
    {
    if ($a<=-10&&$a>=-100)
    echo "two digit negative number";
    }
    ?>

    ReplyDelete
  3. MANSI DUBEY


    =10 && $a<100)
    echo "positive two digit number";
    else
    {
    if($a<=-10 && $a>-100)
    echo "negative two digit number";

    else
    echo "neither positive nor negative two digit number";
    }
    ?>

    ReplyDelete
  4. MANSI DUBEY


    18 && $age<35 )
    {
    echo "Group B";
    }
    elseif ($age>35 && $age<60)
    {
    echo "Group C";
    }
    else
    {
    echo "Group D";
    }

    ReplyDelete
  5. 60 then group d?");

    echo "
    ";
    echo "
    ";

    $age=25;

    echo "Age = $age";

    echo "
    ";
    echo "
    ";

    if($age<=18)

    echo "Answer- Age group A";

    elseif($age>18 && $age<=35)

    echo "Answer- Age group B";

    elseif($age>35 && $age<=60)

    echo "Answer- Age group C";

    else

    echo "Answer- Age group D";

    ?>


    ReplyDelete
  6. Display yes, no, cancel Programme
    Shubahm Nandwal

    $a='y';

    if($a=='y')
    echo "YES";
    else if($a=='c')
    echo "CANCEL";
    else if($a=='n')
    echo "NO";
    else

    echo "Please Make A Valid Entry ";

    ReplyDelete
  7. Programme to check divisibility
    Shubham Nandwal

    $a=45;

    if($a%3==0 && $a%5==0)
    {
    if($a%9==0)
    echo ("
    ");
    echo ("
    ");
    echo ("
    ");
    echo "This Number $a Is Divisible With 3, 5 & 9 ";
    echo "
    ";
    echo "
    ";
    echo "
    ";
    }
    else
    {
    echo ("
    ");
    echo ("
    ");
    echo ("
    ");
    echo "This Number $a Is Not Divisible With 3, 5 & 9 ";
    echo "
    ";
    echo "
    ";
    echo "
    ";
    }

    ReplyDelete
  8. AGE GROUP
    Shubham Nandwal

    $age=10;

    if($age<=18)

    echo "AGE GROUP IS A";
    elseif ($age<=35)
    echo "AGE GROUP IS B";
    elseif ($age<=60)
    echo "AGE GROUP IS C";

    else

    echo "AGE GROUP IS D";

    ReplyDelete
  9. // WAP to display yes, no, cancel when user assign 'y', 'n', and 'c' into a variable?

    $ch='n';
    if($ch=='y')
    {
    echo("Yes");

    }
    else if($ch=='n')
    {
    echo("No");

    }

    else
    {
    echo("Cancel");
    }

    ReplyDelete
  10. $age=45;
    if($age<18)
    {
    echo("Group is A");
    }
    else if($age>=18 && $age<35)
    {
    echo("Group is B");

    }
    else if($age>=35 && $age<60)

    {
    echo("Group is C");
    }
    else
    {
    echo("Group is D");
    }

    ReplyDelete
  11. / WAP to check divisibility of number that it is divisible by 3,5 and 9 with all combinations?
    $num1 = 45;
    $num2=44;
    echo("
    ");
    if($num1%3==0 && $num1%5==0 && $num1%9==0)
    {
    echo("Number is divisible by 3 5 and 9");
    }
    else{
    echo("Number is not divisible");
    }
    echo("
    ");
    if($num2%3==0 && $num2%5==0 && $num2%9==0)
    {
    echo("Number is divisible by 3 5 and 9");
    }
    else{
    echo("Number is not divisible");
    }

    ReplyDelete
  12. ishu manglam

    number divisible by 3,5 and 9.

    $num = 45;

    if($num%3==0 && $num%5==0 && $num%9==0)
    {
    echo " The number $num is divisible by 3,5 and 9 ";
    }
    else
    {
    echo "The number is not";
    }

    ReplyDelete
  13. mohammad kabir aliMarch 5, 2022 at 4:34 PM

    mohammad kabir



    $a=903;
    $b=3990;
    $c=94;
    if($a>$b)
    {
    if($a>$c)
    {
    echo "$a is greatest";
    }
    else
    {
    echo "$c is greatest";
    }}
    else
    {
    if($b>$c)
    {
    echo "$b is greatest";
    }
    else
    {
    echo "$c is greatest";

    }
    }

    ReplyDelete
Post a Comment