Science Stream Practicals

Std 12th IT Subject - Skill Oriented Practical (Science Stream)

SOP 5: Write a PHP program to store mark in an array.

SOP 5: Write a PHP program to save marks of English, Hindi, Marathi, Maths and Information Technology in an array for 5 students and display total marks and percentage of each students using ‘foreach’

Source Code: SOP.php

<!DOCTYPE html>
<html>
<head>
    <title>Write a PHP program to store mark in an array.</title>
</head>
<body>
    <h2>Marks of 5 Students</h2>
    <?php
    // Multidimensional array: marks of 5 students
    $students = array(
        "Student 1" => array("English"=>78, "Hindi"=>85, "Marathi"=>74, "Maths"=>92, "IT"=>88),
        "Student 2" => array("English"=>65, "Hindi"=>70, "Marathi"=>60, "Maths"=>80, "IT"=>75),
        "Student 3" => array("English"=>90, "Hindi"=>88, "Marathi"=>85, "Maths"=>95, "IT"=>92),
        "Student 4" => array("English"=>55, "Hindi"=>60, "Marathi"=>58, "Maths"=>65, "IT"=>62),
        "Student 5" => array("English"=>82, "Hindi"=>79, "Marathi"=>84, "Maths"=>91, "IT"=>87)
    );

    // Loop through each student using foreach
    foreach ($students as $name => $marks) {
        echo "<h3>$name</h3>";

        // Display individual subject marks
        foreach ($marks as $subject => $score) {
            echo $subject . " : " . $score . "<br>";
        }

        // Calculate total and percentage
        $total = array_sum($marks);
        $percentage = $total / count($marks);

        echo "Total Marks: " . $total . "<br>";
        echo "Percentage: " . $percentage . "%<br><br>";
    }
    ?>
</body>
</html>

Live Preview