[Solved] Multiply and Divide Numbers in PHP

  

3
Topic starter

You are given a number num1 and a number num2.

Write a PHP script that:

  • Multiplies num1 * num2 if num2 is greater than or equal to num1.
  • Divides num1 / num2 if num1 is greater than num2.

The input comes as parameters named num1 and num2. Print the output in the HTML page.

Examples:

multphonum2
1 Answer
2

Here is the code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
<form>
    N: <input type="text" name="num1"/>
    M: <input type="text" name="num2"/>
    <input type="submit"/>
</form>
 
<?php
if (isset($_GET['num1']) && isset($_GET['num2'])) {
    $num1 = $_GET['num1'];
    $num2 = $_GET['num2'];
    if ($num2 >= $num1) {
        echo $num1 * $num2;
    } else if ($num1 >= $num2) {
        echo $num1 / $num2;
    }
}
?>
 
</body>
</html>
Share: