التخطي إلى المحتوى الرئيسي

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

?>

تعليقات

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

    ردحذف
  2. Kritika Barod


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

    ردحذف
  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";
    }
    ?>

    ردحذف
  4. MANSI DUBEY


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

    ردحذف
  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";

    ?>


    ردحذف
  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 ";

    ردحذف
  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 "
    ";
    }

    ردحذف
  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";

    ردحذف
  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");
    }

    ردحذف
  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");
    }

    ردحذف
  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");
    }

    ردحذف
  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";
    }

    ردحذف
  13. 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";

    }
    }

    ردحذف

إرسال تعليق

POST Answer of Questions and ASK to Doubt

المشاركات الشائعة من هذه المدونة

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...