Google Ad

HSC Server Side Scripting (PHP) SOP Practicals
with codes and output


(Q1)

Write a PHP program to check if a person is eligible to vote or not. The program should include the following-

  • Minimum age required for vote is 18.
  • Use PHP functions.
  • Use Decision making statement.
Answer:-

<!DOCTYPE html>
<html>
<head>
    <title>Eligible to Vote or not</title>
</head>
<body>
<form action="" method="post">
    Enter a no : 
    <input type="text" name="t1" placeholder="Enter a number">
    <br><input type="submit" name="submit" value="submit">
</form>
<?php 
if (isset($_POST['submit'])) {
    vote();
}
function vote()    {
    $a = $_POST['t1'];
    intval($a);
    if($a>=18){
    echo "You are Eligible for Vote";        
    }    
    else{
    echo "You are not Eligible for Vote";
    }
}
?>
</body>


</html>

Output :

Write a PHP program to check if a person is eligible to vote or no

 

 

(Q2)

Write a PHP function to count the total number of vowels (a,e,i,o,u) from the string. Accept a string by using HTML form.

Answer:-

<html>
<body>
    <h2>Find Number of Vowels in a String</h2>
    <form action="" method="post">
        <input type="text" name="string" />
        <input type="submit" />
    </form>
</body>
</html>

<?php if($_POST)
    {
        $string = strtolower($_POST['string']);
        $vowels = array('a','e','i','o','u');
        $len = strlen($string);
        $num = 0;
        for($i=0; $i<$len; $i++){
            if(in_array($string[$i], $vowels))
            {
                $num++;
            }
        }
         echo "Number of vowels : ".$num;
    } ?>

Output

Write a PHP program to check if a person is eligible to vote or not

(Q3)

Write a PHP program to perform the following operations on an associative array.

  • Display elements of an array along with their keys.
  • Display the size of an array.
  • Delete an element from an array from the given index.
Answer:-

<?php 
    //Display elements of an array along with their keys
$age = array("Ramesh"=>"51", "Rakesh"=>"32", "RAhul"=>"23");

foreach($age as $x => $x_value) {
    echo "Key = " . $x . ", Value = " . $x_value;
    echo "<br>";
}
    //Display the size of an array
    echo "<br>Size of Array is : ".count($age)."<br>";

    //Delete an element from an array from the given index
        $data = array(1,2,3,4,5);
        for ($i=0; $i < count($data); $i++) { 
            echo " ".$data[$i];
        }
        unset($data[1]);
        echo "<br>Elements After Deleting : <br>";
        for ($i=0; $i < count($data); $i++) { 
            echo " ".$data[$i];
        }
 ?>

Output

Write a PHP program to perform the following operations on an associative array.

(Q4)

Write a PHP program to save marks of English, Hindi, Marathi, Maths and Information Technology in an array. Display marks of individual subject along with total marks and percentage

Answer:-

<?php 

    $subject_marks = array('English' =>56 ,'Hindi' =>76,
        'Marathi'=>56,'Maths' =>57 ,'IT'=>78);
    $total_marks = $subject_marks['English'] + 
               $subject_marks['Hindi'] + $subject_marks['Marathi'] +
               $subject_marks['Maths'] + $subject_marks['IT'];

    echo "English : ".$subject_marks['English'];
    echo "<br>Hindi : ".$subject_marks['Hindi'];
    echo "<br>Marathi : ".$subject_marks['Marathi'];
    echo "<br>Maths : ".$subject_marks['Maths'];
    echo "<br>IT : ".$subject_marks['IT'];
    echo "<br><br>Total : ".$total_marks;
    $percentage = $total_marks/5;
    echo "<br>Percentage : ".$percentage;
 ?>

 

Output

PHP program to save marks of English, Hindi, Marathi, Maths and Information Technology in an array

(Q5)

Write a program using PHP to calculate Electricity bill by accepting the limits.

  • For first 100 units – Rs. 4
  • For next 100 units – Rs. 5
  • For next all units – Rs. 6
Answer:-

<!DOCTYPE html>
<head>
    <title>Calculate Electricity Bill</title>
</head>
<?php
$result_str = $result = '';
if (isset($_POST['unit-submit'])) {
    $units = $_POST['units'];
    if (!empty($units)) {
        $result = calculate_bill($units);
        $result_str = 'Total amount of ' . $units . ' - ' . $result;
    }
}
function calculate_bill($units) {
    $unit_cost_first = 4;
    $unit_cost_second = 5;
    $unit_cost_third = 6;

    if($units <= 100) {
        $bill = $units * $unit_cost_first;
    }
    else if($units > 100 && $units <= 200) {
        $temp = 100 * $unit_cost_first;
        $remaining_units = $units - 100;
        $bill = $temp + ($remaining_units * $unit_cost_second);
    }
    else {
        $temp = (100 * 4) + (100 * $unit_cost_second) + (100 * $unit_cost_third);
        $remaining_units = $units - 200;
        $bill = $temp + $remaining_units ;
    }
    return number_format((float)$bill, 2, '.', '');

?>
 
<body>
    <div id="page-wrap">
        <h1>Php - Calculate Electricity Bill</h1>
        
        <form action="" method="post" id="quiz-form">            
                <input type="number" name="units" id="units" placeholder="Please enter no. of Units" />            
                <input type="submit" name="unit-submit" id="unit-submit" value="Submit" />        
        </form>
 
        <div>
            <?php echo '<br />' . $result_str; ?>
        </div>    
    </div>
</body>
</html>

 

Output

program using PHP to calculate Electricity bill by accepting the limits

(Q6)

Write a PHP Program to insert a roll number and student name in a database (use postgresql data to create database). Accept roll number and name from the user.

Answer:-

<!DOCTYPE html>
<html>
<body>
    <h1>Students Detail</h1>
    <form action="" method="post">
        Enter roll no : <br>
        <input type="text" name="r1"><br>
        Enter your name : <br>
        <input type="text" name="t1"><br>
        <input type="submit" name="submit" value="submit">
    </form>
    <?php 
        $host_name="localhost";
        $user_name="root";
        $password="";

       $connect = mysqli_connect($host_name,

$user_name,$password);
        
    if (isset($_POST['submit'])) {
        $roll_no=$_POST['r1'];
        $name=$_POST['t1'];        
        $sql = "insert into students_table values('".$roll_no."','".$name."')";
        $result = mysqli_query($connect,$sql);

        if ($result) {
            echo "Data inserted";
        }
        else{
            echo "Not inserted";
        }
    }

    ?>
</body>
</html>

Output

>Write a PHP Program to insert a roll number and student name in a database