PHP Practical Slip 13 Q.A

Write a PHP Script which will perform the Addition, Subtraction, Multiplication,
and Division of two numbers as per the choice. (Use Switch Case).

<?php
// Define two numbers
$num1 = 20;
$num2 = 5;

$choice = 3; // Change this value to test other operations

switch ($choice) {
    case 1:
        $result = $num1 + $num2;
        echo "Addition: $num1 + $num2 = $result";
        break;

    case 2:
        $result = $num1 - $num2;
        echo "Subtraction: $num1 - $num2 = $result";
        break;

    case 3:
        $result = $num1 * $num2;
        echo "Multiplication: $num1 × $num2 = $result";
        break;

    case 4:
        if ($num2 != 0) {
            $result = $num1 / $num2;
            echo "Division: $num1 ÷ $num2 = $result";
        } else {
            echo "Division by zero is not allowed.";
        }
        break;

    default:
        echo "Invalid choice. Please select 1, 2, 3, or 4.";
}
?>
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top