Practical 2 | PHP Lab Manual (22619) | Write a PHP program to demonstrate the use of Decision-making control structures using- a. If statement b. If-else statement c. Switch statement

 Web Based Application Development with PHP

(22619) Answers



Practical 2: Write a PHP program to demonstrate the use of Decision-making control structures using-
a. If statement
b. If-else statement
c. Switch statement

* Program code 
1) Write a program to find given number is Even or Odd
Ans=> 


  1. <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Even Odd</title>
    </head>
    <body>
        <h1>Even Odd Number</h1>
    <?php
    $num=2;
    if($num%2==0){
        echo("Number is Even ");
    }
    else{
        echo("Number is Even ");
    }
     
    ?>
        </body>
    </html>



* Exerecise 
1. Write a program to make the use of logical operators.
Ans=> 

xample:

  1. <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Logical Operators in php</title>
    </head>
    <body>
        <?php
        $a=10;
        $b=20;
        if($a==10 and $b=20){
            echo(" use of 'and' Logical Operator <br>");
           
        }
        if($a==10 or $b==30){
            echo(" use of 'or' Logical Operator <br>");
        }
        if($a==10 xor $b==50){
            echo(" use of 'xor' Logical Operator <br>");
        }
        if(!$c){
            echo("use of 'not' Logical Operator <br>");
        }
        ?>
       
    </body>
    </html>

2. Write a program to check no is positive or negative.
Ans=> 

xample:

  1. <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Positive Negative  numbeer </title>
    </head>
    <body>
        <?php
        $num=-2;
        if($num>=0)
        {
            echo("<h1>Number is Positive</h1>");
        }
        else{
            echo("<h1> Number is Negative </h1>");
        }
         ?>
    </body>
    </html>
3. Write a calendar program using switch statement.
Ans=> 

xample:

  1. <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Calender using switch statement</title>
    </head>
    <body>
    <?php
    $today = date("D");
    switch($today){
        case "Mon":
            echo "Today is Monday. Clean your house.";
            break;
        case "Tue":
            echo "Today is Tuesday. Buy some food.";
            break;
        case "Wed":
            echo "Today is Wednesday. Visit a doctor.";
            break;
        case "Thu":
            echo "Today is Thursday. Repair your car.";
            break;
        case "Fri":
            echo "Today is Friday. Party tonight.";
            break;
        case "Sat":
            echo "Today is Saturday. Its movie time.";
            break;
        case "Sun":
            echo "Today is Sunday. Do some rest.";
            break;
        default:
            echo "No information available for that day.";
            break;
    }
    ?>
       
    </body>
    </html>

* Practical Related Questions.

1. List operators used in if conditional statement.
Ans=> 1) Comparison Operator.
            2) Logical Operators
            3) Arithmetic Operators 

2. In if-else construct which part will be executed if condition is true.
Ans=> 1) If the condition is true then body of if is executed.

3. State the condition when the else part will be executed with example
Ans=> 
1)The code associated with the 'else' condition is only executed when no other condition has been met .
2) Example :

Example:


  1. <?php
    $num=-5;
    if($num%2==0){
        echo("Number is Even ");
    }
    else{
        echo("Number is Even ");
    }
     
    ?>
     

 .
.
.
.
Keep Growing ❤

Post a Comment

0 Comments