Java, .NET, PHP, PYTHON, ANGULAR, .NET, ML, Data Scince, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

 Conditional Statement in PHP:-


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";
}










?>

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post